Codeforces Round #352 (Div. 1) B. Robin Hood 二分
B. Robin Hood
题目连接:
http://www.codeforces.com/contest/671/problem/B
Description
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.
Sample Input
4 1
1 1 4 2
Sample Output
2
题意
每次你要让最大数减一,然后让最小数加一
然后操作k次
问你最大值减去最小值是多少
题解:
首先这道题模拟是可以的,但是太麻烦了……
所以还是直接二分就好了
二分一个最小值
然后再二分一个最大值
因为你会加k,使得小于那个最小值的数都加成为大于等于他的数
你会减去k,使得大于那个数,都降为小于等于的那个数
所以直接二分就完啦
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
int n,k,a[maxn];
long long sum=0;
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),sum+=a[i];
sort(a+1,a+1+n);
int l1=sum/n,r1=(sum+n-1)/n;
int l=0,r=l1,ansl=0;
while(l<=r)
{
int mid=(l+r)/2;
long long need=0;
for(int i=1;i<=n;i++)if(a[i]<=mid)need+=mid-a[i];
if(need<=k)ansl=mid,l=mid+1;
else r=mid-1;
}
l=r1,r=1e9;
int ansr=0;
while(l<=r)
{
int mid=(l+r)/2;
long long need=0;
for(int i=1;i<=n;i++)if(a[i]>mid)need+=a[i]-mid;
if(need<=k)ansr=mid,r=mid-1;
else l=mid+1;
}
cout<<ansr-ansl<<endl;
}
Codeforces Round #352 (Div. 1) B. Robin Hood 二分的更多相关文章
- Codeforces 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 a ...
- 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& ...
随机推荐
- git fetch 命令
git fetch命令用于从另一个存储库下载对象和引用. 使用语法 git fetch [<options>] [<repository> [<refspec>…] ...
- linux调试工具glibc的演示分析
偶然中发现,下面的两端代码表现不一样 void main(){ void* p1 = malloc(32); free(p1); free(p1); // 这里会报double free ...
- 嵌入式Linux截图工具gsnap移植与分析【转】
转自:http://blog.csdn.net/lu_embedded/article/details/53934184 版权声明:开心源自分享,快乐源于生活 —— 分享技术,传递快乐.转载文章请注明 ...
- 使用dork脚本来查询Google
使用dork脚本来查询Google 了解Google Hacking数据库的第一步是了解所有典型的Google运算,就像机器级编程工程师必须了解计算机操作代码一样. 这些Google运算是Google ...
- Mysql 监控性能状态 QPS/TPS【转】
QPS(Query per second) 每秒查询量 TPS(Transaction per second)每秒事务量 这是Mysql的两个重要性能指标,需要经常查看,和Mysql基准测试的结果对比 ...
- ModelState验证部分属性
ModelState.Remove("Name") 去掉不需要验证的属性.
- 组合比较符(PHP7+)
php7+支持组合比较符,即<=>,英文叫做combined comparison operator,组合比较运算符可以轻松实现两个变量的比较,当然不仅限于数值类数据的比较. 语法:$a& ...
- require和import的区别
require:是一种common协议,大家按照这个约定书写自己的代码,实现模块化. import:是ES6的模块语法实现.是语言自身的模块实现.
- angular可自定义的对话框,弹窗指令
指令不明的,推荐 AngularJS指令参数详解 github地址 以下为示例代码 <!DOCTYPE html> <html lang="en" ng-app= ...
- Effective STL 学习笔记 Item 18: 慎用 vector<bool>
vector<bool> 看起来像是一个存放布尔变量的容器,但是其实本身其实并不是一个容器,它里面存放的对象也不是布尔变量,这一点在 GCC 源码中 vector<bool> ...