题意:有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. DateTime.ToString()

       /*            [y]代表年份,注意是小写的y,大写的Y并不代表年份.            [M]表示月份.            [d]表示日期,注意D并不代表什么.       ...

  2. 总有你需要的之 ios 小技巧 (下)

    图片上绘制文字 写一个UIImage的category NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultPara ...

  3. mybatis 中mapper 的namespace有什么用

    原文:http://zhidao.baidu.com/link?url=ovFuTn7-02s7Qd40BOnwHImuPxNg8tXJF3nrx1SSngNY5e0CaSP1E4C9E5J6Xv5f ...

  4. STL,ATL,WTL之间的联系和区别

    STL即 Standard Template Library (标准模板库) STL是惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Lee和David R M ...

  5. JAVA面试题:equals()方法和== 区别

    http://bbs.csdn.net/topics/390000725 总结: equals在没重写之前和==一样,重写之后,equals只要内容一样即为true equals跟==一般情况下是等价 ...

  6. NPOI.dll学习

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 简介 编辑 使用 NPOI 你就可以在没有安装 Office 或者相应环 ...

  7. HeadFirst设计模式之组合模式

    一. 1.The Composite Pattern allows us to build structures of objects in the form of trees that contai ...

  8. Oracl 动态执行表不可访问,本会话的自动统计被禁止

    oracle ---建立SQL窗体 写入 select * from tableA; 弹出错误窗口 : 动态执行表不可访问,本会话的自动统计被禁止.在执行菜单里你可以禁止统计,或在v$session, ...

  9. 214. Shortest Palindrome

    题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...

  10. Cygwin安装时,选择163的源后出错:Unable to get setup.ini from <http://mirrors.163.com/cygwin/>

    [问题] 折腾: [记录]Cygwin下把make从v3.82换成v3.81 期间,选择了163的源,结果出错: Cygwin Setup Unable to get setup.ini from & ...