Codeforces Round #352 (Div. 2) D 二分



链接:戳这里


D. Robin Hood
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.

There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.

After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.

Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.

Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.

The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person.

Output
Print a single line containing the difference between richest and poorest peoples wealth.

Examples
input
4 1
1 1 4 2
output
2
input
3 1
2 2 2
output
0
Note
Lets look at how wealth changes through day in the first sample.

[1, 1, 4, 2]
[2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2

In second sample wealth will remain the same for each person.


题意:

给出n个人,每个人有一个初始的价值ai,给出k个操作,每个操作必须从当前最富有的那个人拿一个单位的价值给当前最穷的人,问在经历k次操作之后,贫富差距多大


思路:

每次都要从最富的人那里拿一个给最穷的人,所以每次的最穷最富的人都在跟新

简化一下题意,从富有的一部分人那里拿出k给部分穷人,而且穷富人可以来回切换

我们分别找出L,R

L表示穷人经过k次操作之后价值均等于L (L最大

R表示富人经过k次操作之后价值均等于R(R最小

R-L>0  anw=R-L

R-L<0 anw=(a1+a2+...+an)/n==0?0:1  因为经过k此操作之后有些穷人变成了富人,所以在R~L之间存在一个值使得序列相等,不相等的话也就相差1


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,k;
int a[500100];
ll solve1(int x){
    ll ans=0;
    for(int i=1;i<=n;i++){
        if(a[i]<x) ans+=1LL*(x-a[i]);
    }
    return ans;
}
ll solve2(int x){
    ll ans=0;
    for(int i=1;i<=n;i++){
        if(a[i]>x) ans+=1LL*(a[i]-x);
    }
    return ans;
}
int main(){
    scanf("%d%d",&n,&k);
    ll sum=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
    int l=1,r=1e9,mid,ans1=1,ans2=1;
    while(l<r){
        mid=(l+r)/2;
        if(solve1(mid)<=(ll)k) l=mid+1,ans1=mid;
        else r=mid;
    }
    l=1;r=1e9;
    while(l<r){
        mid=(l+r)/2;
        if(solve2(mid)<=(ll)k) r=mid,ans2=mid;
        else l=mid+1;
    }
    if(ans2-ans1>0) cout<<ans2-ans1<<endl;
    else printf("%d\n",sum%n==0?0:1);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值