C. Maximum Median
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 kk operations.
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
Copy
3 2 1 3 5
output
Copy
5
input
Copy
5 5 1 2 1 1 1
output
Copy
3
input
Copy
7 7 4 1 2 4 3 4 4
output
Copy
5
Note
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 , 接下来给出长度为 n 的序列,你可以将序列中的任意值 + 1 , 但是这个操作不能超过 k 次,问最大中位数是多少?
思路:根据题意想使中位数变大,根据贪心策略首先排序,然后当然是直接从中间开始加最好,如果直接给中位数加上 k ,那么它可能的结果是要么加上去之后仍然为中位数,值变成 a[mid] + k ,要么后面的某个数成为中位数(因为这个数加上之后比他大),则相比之下多出来的一部分(设后面被挤下来的数为 q),那么a[mid] + k - q就是没必要加的,因为加了也没什么好处,事实上就是将这个数变成了后面的某个数,以此类推,若我们想要有一个更大的中位数,我们得先至少慢慢成为我们序列中已经存在的更大的数,这个我们必须要挨个扫描得出的,而使我们当前这个更大的数必须也要将我们扫描的所有数也变成它。那么k在整个操作中有两个奇异状态,一是中途不够加了,二是加完了还有多的。对于第一种情况我们就是看它在中途停的那个地方,是否能让这儿连续区间同时增加几个数量,从而使得中位数变大。第二种情况与第一种情况类似,只不过是最后退出的时候判断的,如果最后退出了还有多余的k,那么我们当然是看他是否能在次为整个变化后右区间的数整体加值,直接剩余的数量除原来的一半就好了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
void init_cin(){ //*
std::ios::sync_with_stdio(false); //*
std::cin.tie(0); //*
std::cout.tie(0); //*
}
priority_queue<ll,vec,greater<ll> > q;
int solve(int k)
{
if(k) return puts("YES");
else return puts("NO");
}
int main()
{
init_cin();
vec v;
v.push_back(-1);
ll n , k;
ll has = 1;
cin >> n >> k;
for(int i = 1 ; i <= n ; ++ i)
{
ll doll;
cin >> doll;
v.push_back(doll);
}
sort(v.begin(),v.end());
int bgn = (n + 1)/ 2;
ll toper = v[bgn];
int i;
for( i = bgn + 1; i <= n ; ++ i)
{
ll dx = min(k/has,v[i] - toper);
toper += dx;
k -= dx * has;
++ has;
}
if(k > 0) toper += k / bgn;
cout << toper << endl;
}