题意:有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 二分的更多相关文章

  1. 【模拟】Codeforces 671B Robin Hood

    题目链接: http://codeforces.com/problemset/problem/671/B 题目大意: N个人,每个人有Ci钱,现在有一个人劫富济贫,从最富的人之一拿走1元,再给最穷的人 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces 672D Robin Hood(二分好题)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. 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 ...

  6. codeforces 672D D. Robin Hood(二分)

    题目链接: D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)

    题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...

  8. CodeForces 672D Robin Hood

    思维. 当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$.我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算. 需要计算的就是,将最小的几个数总共加$k$次,最小值最大 ...

  9. 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 ...

随机推荐

  1. HDFS(Hadoop Distributed File System )

    HDFS(Hadoop Distributed File System ) HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表 ...

  2. Nhibernate 一对一关系映射(主键映射)

    参考:点击这里 妈的,搞了一天了,终于可以了,现在总结下,以防下次再出现这样痛苦的问题了,有两个表:user(用户)和Blog(设置表),它们之间的关系正如我所说的是一对一的关系.现在我们来映射这两个 ...

  3. CSS两列及三列自适应布局方法整理

    布局 自适应 两列 三列 在传统方法的基础上加入了Flex布局并阐述各方法的优缺点,希望对大家有所帮助.先上目录: 两列布局:左侧定宽,右侧自适应 方法一:利用float和负外边距 方法二:利用外边距 ...

  4. Kerbose

    http://blog.csdn.net/wulantian/article/details/42418231

  5. linux 模拟延时和丢包

    这是 RHCA 中的一个 BDP 的测试,这也是公司很常用的一种延时和丢包的模拟,现在分享给大家. 我们做的应用软件,还有测试 TCP/UDP  对比,测试 BDP 对 TCP/IP 的影响时,我们都 ...

  6. Servlet中乱码问题

    页面编码方式为utf-8 当使用post传值时 request.setContentType("utf-8"); 当使用get传值时 String str = request.ge ...

  7. 【mysql的设计与优化专题(5)】慢查询详解

    查询mysql的操作信息 show status -- 显示全部mysql操作信息 show status like "com_insert%"; -- 获得mysql的插入次数; ...

  8. spring transactionmanager

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  9. live555源码研究(四)------UserAuthenticationDatabase类

    一.UserAuthenticationDatabase类作用 1,用户/密码管理 2,鉴权管理 二.类UserAuthenticationDatabase继承关系图                 ...

  10. MSSQLServer基础02(SQL语句入门(脚本、命令))

    SQL 全名是结构化查询语言(Structured Query Language),是关系数据库管理系统的标准语言 SQL语句是和DBMS“交谈”专用的语句,不同DBMS都认SQL语法. SQL语句中 ...