codeforces 672D 二分

ゞ 浴缸里的玫瑰 2022-07-24 07:24 244阅读 0赞

D. 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 c**i 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 c**i (1 ≤ c**i ≤ 109) — initial wealth of the i-th person.

Output

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

Examples

input

  1. 4 1
  2. 1 1 4 2

output

  1. 2

input

  1. 3 1
  2. 2 2 2

output

  1. 0

Note

Lets look at how wealth changes through day in the first sample.

  1. [1, 1, 4, 2]
  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天之后最富有的人跟最少财富的人的差值是多少。

分析: 可以二分的查找最富有的人的钱数和最少财富的人的钱数,然后两者相减就是结果,在二分之前还有确定最多的钱的下界和最少的钱的上界。如果总钱数sum能整除n,那么上界跟下界都可以取到sum/n,否则上界能取到sum/n, 下界能取到sum/n加1。有了上下界, 就可以进行二分了。

  1. #include <bitset>
  2. #include <map>
  3. #include <vector>
  4. #include <cstdio>
  5. #include <iostream>
  6. #include <cstring>
  7. #include <string>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <stack>
  11. #include <queue>
  12. #include <set>
  13. #define inf 0x3f3f3f3f
  14. #define mem(a,x) memset(a,x,sizeof(a))
  15. #define F first
  16. #define S second
  17. using namespace std;
  18. typedef long long ll;
  19. typedef unsigned long long ull;
  20. typedef pair<int,int> pii;
  21. inline int in()
  22. {
  23. int res=0;char c;int f=1;
  24. while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
  25. while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
  26. return res*f;
  27. }
  28. const int N=510000,MOD=1e9+7;
  29. int a[N],n,k;
  30. bool check1(int x)
  31. {
  32. ll sum=0;
  33. for(int i=0;i<n;i++){
  34. if(a[i]<x) sum += x-a[i];
  35. }
  36. return sum <= k;
  37. }
  38. bool check2(int x)
  39. {
  40. ll sum=0;
  41. for(int i=0;i<n;i++){
  42. if(a[i]>x) sum += a[i]-x;
  43. }
  44. return sum <= k;
  45. }
  46. int main()
  47. {
  48. n=in(),k=in();
  49. ll sum=0;
  50. for(int i=0;i<n;i++){
  51. a[i]=in();
  52. sum += a[i];
  53. }
  54. ll minup,maxdown;
  55. if(sum % n == 0){
  56. minup = maxdown = sum/n;
  57. }
  58. else{
  59. minup = sum/n;
  60. maxdown = minup+1;
  61. }
  62. ll l=1,r=minup;
  63. ll ans;
  64. while(l<=r){
  65. int mid = l+r>>1;
  66. if(check1(mid)) l=mid+1,ans=mid;
  67. else r=mid-1;
  68. }
  69. l=maxdown,r=1e9;
  70. ll tmp;
  71. while(l<=r){
  72. int mid=l+r>>1;
  73. if(check2(mid)) r=mid-1,tmp=mid;
  74. else l=mid+1;
  75. }
  76. ans = tmp-ans;
  77. cout<<ans<<endl;
  78. return 0;
  79. }

发表评论

表情:
评论列表 (有 0 条评论,244人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CodeForces1214D

    [CodeForces1214D][] 这个题据我所知有两种比较优秀的做法. 第一种是\\(DP\\)统计每个点的路径数,然后找出必经点,再从必经点开始\\(bfs\\

    相关 CodeForces1208D

    CodeForces1208D 也是挺吓人的一道题,我一开始以为给的是每个数字前比它小的数字有几个,然后我就苦苦看不懂样例... 然后我冷静了一下,重新分析,读题,发现

    相关 codeforces 567D

    满足二分的条件,如果当前的断点不符合条件,那么后面的一定不符合条件,如果当前的符合条件,那么后面的可能还有符合条件的。 二分的过程中对断点进行排序,判断每个区间能放多少

    相关 Codeforces 496D

    题意 -------------------- 进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛