您的位置:首页 > 其它

cf 671/B Robin Hood(二分)@

2017-05-22 11:52 330 查看
Robin Hood

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard 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个人,每个人ci的金币,每天最富有的人都会给最贫穷的人1金币,问k天后最富有人和最贫穷的人差了多少金币。

解:二分枚举最小值得最大值,和最大值的最小值,因为二者都是在K的允许范围内操作,所以一定可以中和,如果最小值大于了最大值,那么就是相遇情况考虑综合是否能被N整除

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<cmath>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+7;
typedef long long LL;
const int inf = 0x3f3f3f3f;
char str[1000];
LL a
, b
;
int cmp(LL A,LL B)
{
return A>B;
}
int n;
LL k, k2;
int judge1(LL x)
{
int i;
LL sum=0, sum1=0;
for(i=0;i<n;i++)
{
if(a[i]<x)  sum+=(x-a[i]);
else break;
if(sum>k) return 0;
}
if(sum>k) return 0;
return 1;
}
int judge2(LL x)
{
LL sum=0, sum1=0;
int i;
for(i=0;i<n;i++)
{
if(b[i]>x) sum+=(b[i]-x);
else break;
if(sum>k) return 0;
}
if(sum>k) return 0;
return 1;
}

int main()
{
LL sum=0;
scanf("%d %I64d", &n, &k);
for(int i=0;i<n;i++)
{
scanf("%I64d", &a[i]);
b[i]=a[i], sum+=a[i];
}
sort(a,a+n);
sort(b,b+n,cmp);
if(k==0)
{
cout<<b[0]-a[0]<<endl;
return 0;
}
LL l=a[0], r=sum, ans1, ans2;
while(l<=r)
{
LL mid=(l+r)/2;
if(judge1(mid)) l=mid+1,ans1=mid;
else r=mid-1;
}
l=a[0], r=sum;
while(l<=r)
{
LL mid=(l+r)/2;
if(judge2(mid)) r=mid-1,ans2=mid;
else l=mid+1;
}
if(ans2>ans1) cout<<ans2-ans1<<endl;
else printf("%d\n",sum%n!=0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: