codeforces 671B Robin Hood 二分
题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人
问k次操作以后,物品最多的人和物品最少的人相差几个物品
分析:如果次数足够多的话,最后的肯定在平均值上下,小的最多被补到sum/n,大最多减少到sum/n,或者sum/n+1
然后就二分最小值,看所有小的是否能在k次被填满
二分最大值,看所有大的是否都在k次被抹平
然后就是二分的写法,小和大的不一样,需要加等号,避免死循环
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = 5e5+;
int a[N],k,n;
bool checkmax(int x){
int tmp=k;
for(int i=;i<=n;++i){
if(a[i]>x)tmp-=(a[i]-x);
if(tmp<)return false;
}
return true;
}
bool checkmin(int x){
int tmp=k;
for(int i=;i<=n;++i){
if(a[i]<x)tmp-=(x-a[i]);
if(tmp<)return false;
}
return true;
}
int main(){
scanf("%d%d",&n,&k);
LL sum=;
int k1,k2,x=,y=0x7f7f7f7f;
for(int i=;i<=n;++i){
scanf("%d",&a[i]);
sum+=a[i];
x=max(x,a[i]);
y=min(y,a[i]);
}
k1=k2=sum/n;
if(sum%n)++k2;
int ans2,ans1,l=y,r=k1;
while(l<=r){
int m=(l+r)>>;
if(checkmin(m))ans1=m,l=m+;
else r=m-;
}
l=k2,r=x;
while(l<r){
int m=(l+r)>>;
if(checkmax(m))r=m;
else l=m+;
}
ans2=(l+r)>>;
printf("%d\n",ans2-ans1);
return ;
}
codeforces 671B Robin Hood 二分的更多相关文章
- 【模拟】Codeforces 671B Robin Hood
题目链接: http://codeforces.com/problemset/problem/671/B 题目大意: N个人,每个人有Ci钱,现在有一个人劫富济贫,从最富的人之一拿走1元,再给最穷的人 ...
- 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 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 672D D. Robin Hood(二分)
题目链接: D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- CodeForces 672D Robin Hood
思维. 当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$.我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算. 需要计算的就是,将最小的几个数总共加$k$次,最小值最大 ...
- 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 ...
随机推荐
- 在百万数据中找出重复的数据sql
select * from (select count(name) as isone, name from tbl_org_departments group by name) t where t.i ...
- 第三方登录过程—OAuth2.0协议
---恢复内容开始--- 理清思路 1.在第三方注册成为开发者,拿到第三方给的client_id(app_id---就像你的身份证.QQ号)和client_secret(就像你的QQ密码): 2.填写 ...
- Delaunay三角剖分
Bowyer-Watson算法:1.假设已经生成了连接若干个顶点的Delaunay三角网格:2.加入一个新的节点,找出所有外接圆包含新加入节点的三角形,并将这些三角形删除形成一个空洞:3.空洞的节点与 ...
- VS asp.net 连接64位oracle 11g
vs2010 vs2013 vs2015 无法连接oracle 11g 64bit 尝试加载 Oracle 客户端库时引发 BadImageFormatException......... A.安装o ...
- uva 11151
求最长回文串 就是将字符串翻转后求最长公子列..... #include <cstdio> #include <cstdlib> #include <algorithm ...
- 【C++基础】sizeof 数组 指针 空NULL
笔试遇到很多sizeof的小题,博主基础堪忧,怒总结如下,还是要巩固基础啊啊啊! sizeof操作符 对象所占 栈内存空间的大小,单位是字节 关键词:char 数组 指针 结构体 class [注意 ...
- Nagios的安装配置与应用之五监控远程Linux服务器
本文出自 “曹坏水” 博客,请务必保留此出处http://cao2012.blog.51cto.com/366908/1132113 NRPE是Nagios的一个功能扩展,它可在远程Linux和UNI ...
- 用CodeViz绘制函数调用关系图(call graph)
CodeViz是<Understanding The Linux Virtual Memory Manager>(at Amazon,下载地址在页尾)的作者 Mel Gorman 写的一款 ...
- CodeForces 279B Books
http://codeforces.com/problemset/problem/279/B 题意 :Valera 有很多的空闲时间,所以他决定看书,给出n本书,编号1到n,和看每本书需要的时间,他看 ...
- objective-c 在线视频 学习资料...
---视频 http://www.lanou3g.com/newslist.php?cid=7 http://edu.51cto.com/lesson/id-15489.html http://www ...