Codeforces Round #352 (Div. 1) B. Robin Hood
来源:互联网 发布:php登录页面代码 编辑:程序博客网 时间:2024/06/05 21:59
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.
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.
Print a single line containing the difference between richest and poorest peoples wealth.
4 11 1 4 2
2
3 12 2 2
0
#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<set>#include<map>#include<time.h>#include<cstdio>#include<vector>#include<stack>#include<queue>#include<iostream>using namespace std;#define LONG long longconst int INF=0x3f3f3f3f;const int MOD=1e9+7;const double PI=acos(-1.0);#define clrI(x) memset(x,-1,sizeof(x))#define clr0(x) memset(x,0,sizeof x)#define clr1(x) memset(x,INF,sizeof x)#define clr2(x) memset(x,-INF,sizeof x)#define EPS 1e-10int n ;LONG c[500000+20];LONG calcu1(int x ){ LONG res =0 ; for(int i = 1 ;i <=n ; ++ i) if(c[i] < x) res += (LONG )(x - c[i]); else break ; return res ;}LONG calcu2(int x){ LONG res = 0; for(int i = n; i >= 1 ;-- i) if(c[i] > x) res += (LONG )(c[i] - x); else break ; return res ;}int main(){ LONG k ; scanf("%d%lld",&n,&k); LONG sum = 0; for( int i =1; i<= n ;++ i) { scanf("%lld",&c[i]); sum += c[i] ; } sort(c + 1 , c + n + 1); int l = c[1] , r = c[n]; int mid ; while(l < r) { mid = (l + r + 1)/2; if(calcu1(mid) > (LONG )k) r = mid - 1; else l = mid ; } int x = l; //最穷的人有多少钱 l = c[1] , r = c[n]; while(l < r) { mid = (l + r ) / 2; if(calcu2(mid) > (LONG )k) l = mid + 1; else r = mid ; } int y = l; if( x >= y) { if(sum % n == 0) cout<<0<<endl; else cout<<1<<endl; } else cout<< y - x<<endl;}
- Codeforces Round #352 (Div. 1) B. Robin Hood
- Codeforces Round #352 (Div. 1) B. Robin Hood
- Codeforces Round #352 (Div. 1) B. Robin Hood
- Codeforces Round #352 (Div. 2) Robin Hood
- Codeforces Round #352 (Div. 1) B. Robin Hood (以后二分就这么写吧)
- Codeforces Round #352 (Div. 1) B. Robin Hood(想法+二分,好题)
- codeforces 671B /Round #352(div.2) D.Robin Hood 二分
- Codeforces Round #352 (Div. 2) D. Robin Hood
- Codeforces Round #352 (Div. 2) D Robin Hood : 模拟
- CodeForces 671B Robin Hood
- CodeForces Round 352——Recycling Bottles & Robin Hood
- codeforces #352(div1)-Robin Hood
- Codeforces 671B Robin Hood 二分答案
- Codeforces 671B Robin Hood 【思维】
- 【模拟】Codeforces 671B Robin Hood
- Codeforces 671B Robin Hood (二分搜索)
- CodeForces 671 B.Robin Hood(水~)
- Codeforces 672D Robin Hood
- C语言调用Lua编译问题总结
- (转载)机器学习知识点(十五)从最大似然到EM算法浅解
- Java23种设计模式:Iterator设计模式(三)
- dblink跨域查询数据库
- Sping容器与被管的Bean
- Codeforces Round #352 (Div. 1) B. Robin Hood
- 用C语言写面向的对象是一种什么样的体验
- mtk8735 mtk8163 mtk8127 mtk6735量产方案 核心板方案 mtk系列平台
- Android NDK Tools 下载链接大全
- Redis入门详解
- 公共场所wifi认证解决方案wifidog+authpuppy
- 5. Longest Palindromic Substring
- Python脚本收集腾讯云CDN日志,并入ELK日志分析
- MyISAM和InnoDB的区别