codeforces 671B
来源:互联网 发布:mac可以玩守望先锋吗 编辑:程序博客网 时间:2024/06/09 20:23
B. 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个人,另外一个人有k天时间做一件事情:把最富的一个人的一块钱给最穷的一个人,问你在k天过后最富的人与最穷的人的最小钱差距为多少.
方法:首先我们要知道怎样才能使最富的人与最穷的人的最小钱差距为多少.图无法放上去,只有直接写了.首先我们可以先找一条线使所有re,我们可以先找一个平均值,显然如达到了最佳情况,肯定所有人的钱数都在这个点或+1.然后我们就可从平均值往下二分让最穷的人钱越多,在往上二分让最富的人钱越少.
#include <cstdio>#include <cmath>#include <iostream>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int n,k,a[500005];long long prt[500005],m,sum,mini,maxi,aer;void readdata(){ scanf("%d%d",&n,&k); for (int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } aer=sum/n;}bool checkl(int x){ int p=lower_bound(a,a+n,x)-a-1; if ((ll)x * (p + 1) - prt[p] > k) return false; else return true;}bool checkr(int x){ int p=upper_bound(a,a+n,x)-a; if (prt[n-1] - prt[p-1] - (ll)(n - p) * x > k) return false; else return true;}void work(){ sort(a,a+n); int l=a[0]; int r=aer+1; for (int i=0;i<n;i++) { prt[i]=prt[i-1]+a[i]; } while (l+1<r) { int mid=(l+r)>>1; if (checkl(mid)) { l=mid; } else r=mid; } mini=l; l=aer;if (sum%n>0) l++; r=a[n-1]; while (l<r) { int mid=(l+r)>>1; if (checkr(mid)) { r=mid; } else l=mid+1; } maxi=r; cout<<maxi-mini;}int main(){ readdata(); work();}
- codeforces 671B
- codeforces 671B
- Codeforces 671B 二分
- CodeForces 671B Robin Hood
- codeforces B
- codeforces B
- codeforces B
- codeforces B
- Codeforces 671B Robin Hood 二分答案
- Codeforces 671B Robin Hood 【思维】
- 【模拟】Codeforces 671B Robin Hood
- Codeforces 671B Robin Hood (二分搜索)
- CodeForces 671 B.Robin Hood(水~)
- CodeForces 626B CodeForces 626B【暴力】
- CodeForces 841B (B) 博弈
- codeforces 134B
- codeforces#98 b
- codeforces 105 div2 B
- Centos7 下mysql安装配置
- codeforces Round 286# problem A. Mr. Kitayuta's Gift < 回文串 >
- 二叉树的一些笔试面试常见题目
- 17缓冲流原理
- PendingIntent和Intent
- codeforces 671B
- Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations(线段树)
- Ajax跨域请求——jsonp技术
- 96.苹果企业证书过期与Appstore证书与描述文件过期解决方案-描述文件过期的方案
- iOS开发小点之启动广告界面
- git 命令
- 素数筛模板
- 【Codeforces Round 362 (Div 2)A】【简单讨论】Pineapple Incident
- 蘑菇街TeamTalk源码流程思维导图