Codeforces #557 Div2 C题解

题目链接:https://codeforc.es/blog/entry/68911

题意

对与n个数,有m次+1操作,可以选择任意数字进行+1,要求m次操作后,n个数排序后的中位数最大,n满足一定是奇数。
The first line contains two integers n and k (1≤n≤2⋅10^5, n is odd, 1≤k≤10^9) — the number of elements in the array and the largest number of operations you can make.

思路

二分答案
首先排序,再二分答案,对n/2+1到n之间的数计算当前的最小操作,设x为中位数,则最少次数为 ∑ i = n / 2 + 1 n m a x ( 0 , x − n i ) \sum_{i=n/2+1}^nmax(0,x-ni) i=n/2+1nmax(0,xni),当前中位数判断是否满足操作次数<=m,不满足取较小区间,满足取较大区间。

#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define ll long long

int n,m;
void read(int &x)
{
    x=0;
    bool flag=0;
    char ch=getchar();
    if(ch=='-') flag=1;
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')x*=10,x+=ch-'0',ch=getchar();
    if(flag) x=-x;
}

int ff[MAXN];
bool check(int x)
{
    ll sum=0;
    for(int i=n/2+1;i<=n;i++)
        sum+=max(x-ff[i],0);
    if(sum<=m)
        return true;
    else
        return false;
}

int main()
{
    read(n),read(m);
    for(int i=1;i<=n;i++)
        read(ff[i]);

    sort(ff+1,ff+n+1);

	ll l = 0, r = 2000000009;
	while (l<r)
	{
		ll mid = (l + r + 1) / 2;
		if (check(mid))l = mid;
		else
			r = mid - 1;
	}

	printf("%lld",l);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值