Codeforces Round #577 (Div. 2) C. Maximum Median(二分)

题目大意:
给定一个长度为n的数组a,以及额外k次操作。
每次操作都可以把数组中的任意一个数变大一个单位。
问该数组可以得到的最大中位数是?
注意数据范围:
(1≤n≤2*10^5, n is odd, 1≤k≤10^9)
(1≤ai≤10^9).
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
//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<deque>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=500200;
const int M=5002;
const int mod=998244353;
LL a[N],b[N];
int h[N],w[N],e[N],ne[N],dist[N],idx;
int f[M][M];
bool vis[N],st[N];
int dx[]={-1,0,0,1,-1,-1,1,1},dy[]={0,1,-1,0,1,-1,-1,1};
LL n,k,ans=0,zj=0;
bool check(LL mid)
{
    LL need=0;
    for(int i=zj;i<=n;i++)//从中间往后凑,需要凑出最大中间值的需要补上的数量
    {
        if(a[i]<mid) need+=mid-a[i];
        else break;
    }
    return need<=k;
}
int main()
{
    //cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        //cin>>n>>k;
        scanf("%lld %lld",&n,&k);
        for(LL i=1;i<=n;i++)
            scanf("%lld",&a[i]);
            //cin>>a[i];
        sort(a+1,a+1+n);
        zj=n/2+1;
        LL l=1,r=2e9+10;
        while(l<=r)
        {
            LL mid=(l+r)>>1;
            if(check(mid))
            {
                ans=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        printf("%lld\n",ans);
        //cout<<ans<<endl;
    }
    return 0;
}
posted @ 2022-07-28 20:59  高尔赛凡尔娟  阅读(35)  评论(1编辑  收藏  举报