【CodeForces - 1201C】Maximum Median(思维,水题)
題干:
You are given an array?aa?of?nn?integers, where?nn?is odd. You can make the following operation with it:
- Choose one of the elements of the array (for example?aiai) and increase it by?11(that is, replace it with?ai+1ai+1).
You want to make the median of the array the largest possible using at most?kkoperations.
The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array?[1,5,2,3,5][1,5,2,3,5]?is?33.
Input
The first line contains two integers?nn?and?kk?(1≤n≤2?1051≤n≤2?105,?nn?is odd,?1≤k≤1091≤k≤109)?— the number of elements in the array and the largest number of operations you can make.
The second line contains?nn?integers?a1,a2,…,ana1,a2,…,an?(1≤ai≤1091≤ai≤109).
Output
Print a single integer?— the maximum possible median after the operations.
Examples
Input
3 2 1 3 5Output
5Input
5 5 1 2 1 1 1Output
3Input
7 7 4 1 2 4 3 4 4Output
5Note
In the first example, you can increase the second element twice. Than array will be?[1,5,5][1,5,5]?and it's median is?55.
In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is?33.
In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be?[5,1,2,5,3,5,5][5,1,2,5,3,5,5]?and the median will be?55.
題目大意:
? 給你n個數,讓你可以最多執行K次操作,每次操作使得一個數+1,問你操作完之后的序列的中位數最大是多大。
解題報告:
? 水題,排序后發現只跟后一般的數有關。考慮每次操作,操作前半個區間的數字沒有意義。操作后面的數字的話也沒有意義,因為看的是中位數,所以只有中間這個數字變大,答案才能變大。(也就是說使得答案變大的方式只有使得中間這個數字變大)然后模擬這個過程就行了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; ll a[MAX]; map<ll,ll> mp; set<ll> ss; int main() {int n,k;cin>>n>>k;for(int i = 1; i<=n; i++) cin>>a[i];sort(a+1,a+n+1);for(int i = (n+1)/2; i<=n; i++) ss.insert(a[i]),mp[a[i]]++;ll num = mp[a[(n+1)/2]];//當前這個大點集的點的個數 ll ans = a[(n+1)/2];while(k>0) {auto it = ss.upper_bound(ans);if(it == ss.end()) break;ll ci = (*it-ans)*num;if(ci <= k) {k -= ci;ans = *it;num += mp[*it];}else {ans += (k / (num));k = 0; }}if(k > 0) ans += k/(num);cout << ans << endl;return 0 ; } //16:16-16:26總結:剛開始WA了一發,是因為else中k=0和ans+=(k/sum)這兩句寫反了,,,真服了自己了。
總結
以上是生活随笔為你收集整理的【CodeForces - 1201C】Maximum Median(思维,水题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 350万存款被挪用,储户竟要背负60%的
- 下一篇: 【BZOJ - 4337】BJOI201