首页 > 其他分享 >Codeforces1201 B Maximum Median (二分)

Codeforces1201 B Maximum Median (二分)

时间:2023-02-03 10:34:57浏览次数:39  
标签:Codeforces1201 median ll Median Maximum mid increase array include


Description:

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 5

Output

5

Input

5 5 1 2 1 1 1

Output

3

Input

7 7 4 1 2 4 3 4 4

Output

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.

可以对每个数改变K次,每次+1,秋阳最大的中位数,

二分枚举所有可能

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
int i,j,k;
const int maxn = 2e5+100;
ll a[maxn];
ll ans,n;
int judge(ll mid)
{
ll last=0;
for (int i=n/2+1;i<=n;i++ )
{
if(a[i]<=mid )
{
last+=(mid-a[i]);
}
}
if(last<=k)
{
return 1;
}
else
{
return 0;
}
}

int main()
{
cin>>n>>k;
for( i=1; i<=n; i++ )
{
cin>>a[i];
}
sort(a+1,a+n+1);
ll l=1;
ll r=3e9;
while(l<=r)
{
ll mid=(l+r)/2; // 枚举答案,中位数为mid是否符合题意
if(judge(mid))
{
l=mid+1;
ans=mid;
}
else
{
r=mid-1;
}
}
cout<<ans<<endl;
return 0;
}

 

标签:Codeforces1201,median,ll,Median,Maximum,mid,increase,array,include
From: https://blog.51cto.com/u_15952369/6035447

相关文章