Codeforces Round #352 (Div. 2) D. Robin Hood 二分
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 1
1 1 4 2
2
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.
题意:
给你一个序列
每一天过后,序列的最大值-1,最小值+1, 问你k天之后,最大值-最小值是多少
题解:
在给定k天下
我们二分最大值尽可能的 小,最小值最可能的大就可以了
#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+, M = 1e7, mod = 1e9+,inf = 1e9+;
typedef long long ll; int a[N],n,k;
int check1(int mx) {
ll kk = ,buji = ;
for(int i=;i<=n;i++) {
if(a[i]>mx) kk+=(a[i]-mx);
else {
buji+=(mx - a[i]);
}
}
if(kk<=k&&buji>=kk) return ;
else return ;
}
int check2(int mi) {
ll kk = ,buji = ;
for(int i=;i<=n;i++) {
if(a[i]<mi) kk+=(mi - a[i]);
else buji += (a[i] - mi);
}
if(kk<=k&&buji>=kk) return ;
else
return ;
}
int main() {
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
int mx = a[n],mi = a[];
int r = a[n], l = a[];
while(l<=r) {
int mid = (l+r)>>;
if(check1(mid)) r = mid-,mx = mid;
else l = mid+;
}
r = a[n], l = a[];
while(l<=r) {
int mid = (l+r)>>;
if(check2(mid)) l = mid+, mi = mid;
else r = mid-;
}
//cout<<mx<<" "<<mi<<endl;
cout<<mx - mi<<endl;
return ;
}
Codeforces Round #352 (Div. 2) D. Robin Hood 二分的更多相关文章
- Codeforces Round #352 (Div. 1) B. Robin Hood 二分
B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...
- Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- Codeforces 671B/Round #352(div.2) D.Robin Hood 二分
D. Robin Hood We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and ...
- Codeforces Round #352 (Div. 1) B. Robin Hood (二分)
B. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #352 (Div. 1) B. Robin Hood
B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...
- Codeforces Round #352 (Div. 2) D. Robin Hood
题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...
- Codeforces Round #352 (Div. 2) ABCD
Problems # Name A Summer Camp standard input/output 1 s, 256 MB x3197 B Different is Good ...
- Codeforces Round #352 (Div. 2)
模拟 A - Summer Camp #include <bits/stdc++.h> int a[1100]; int b[100]; int len; void init() { in ...
- Codeforces Round #352 (Div. 2) (A-D)
672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...
随机推荐
- svn代码回滚命令
代码回滚提交: 比如要把73回滚到68 svn merge -r 73:68 http://my.repository.com/my/project/trunk 然后commit就行了 svn com ...
- android TP驱动移植调试笔记(转)
1. 添加I2C 设备 TP 一般采用的是I2C 作为数据和命令接口,所以TP 驱动也可以归类为I2C 驱动.TP驱动的主要逻辑不在这里,但是了解了Linux 的I2C 体系架构,就可以对整个驱动流程 ...
- Css transition
CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并圆滑地以动画效果改变CSS的属性值. <!DOCTY ...
- 在ubuntu上面安装phpmyadmin后,报404错误
安装完mysql后,我想装一个phpmyadmin方便mysql管理. 在终端执行命令:sudo apt-get install libapache2-mod-auth-mysql phpmyadmi ...
- 用sql合并列,两句话合为一句
合并bc两列 UPDATE `test` SET `a`=concat(`b`,`c`) 清空a列 UPDATE `test` SET `a` = NULL
- 自动获取wordpress日志中的第一张图片作为缩略图
图片在博客中算是吸引访客阅读欲望的一种方法,在日志列表如果有一张吸引力十足的图片作为缩略图,70%的游客会点击浏览具体的文章.既然那样,赶紧去加缩略图吧. 我们知道 WordPress 有个日志缩略图 ...
- IIS计数器
Bytes Total/sec 是 Bytes Sent/sec 与 Bytes Received/sec 的总和.这是 Web 服务每秒传输的总字节数. Cache Total Turnover R ...
- unity3d 截屏
原地址:http://www.cnblogs.com/88999660/archive/2013/01/21/2869747.html void OnGUI(){ if(GUI.Button(new ...
- Window_搭建SVN服务器
http://wenku.baidu.com/link?url=614FLi_VlhiJpyG5bq8JcwFBHroMjsV3FvBDzyyn0snZ85jbWx7xh-RPJdH7stxlgM9i ...
- Nginx反向代理的目录访问问题
Nginx反向代理的目录访问问题 2013-05-13 23:21 2730人阅读 评论(0) 收藏 举报 从昨天就开始纠结了,在做实验的时候,遇到目录访问的问题,如下 前端nginx vhost的设 ...