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 ...
随机推荐
- OO之策略模式
以下为策略模式详解: 引子: 使用策略就是要实现可扩展性,那么多态是不可少的.何谓可扩展性呢? 比如:我们用面向对象的思想来设计飞机,基类为飞机,飞机可以有很多种,客机,直升机,战斗机等,不同种类的飞 ...
- sql之表连接 筛选条件放在 连接外和放在连接里的区别
使用一个简单的例子,说明他们之间的区别 使用的表:[Sales.Orders]订单表和[Sales.Customers]客户表,和上一篇博客的表相同 业务要求:查询出 : 所有的用户 在 2012-1 ...
- 【贪心】 BZOJ 3252:攻略
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 261 Solved: 90[Submit][Status][Discuss] De ...
- MVC中的@Html.DisplayFor等方法如何控制日期的显示格式(转) - Rising
在Sql Server2005中,如果将某字段定义成日期 时间 类型DateTime,那么在视图中会默认显示成年月日时分秒的方式(如 2013/8/6 13:37:33) 如果只想显示成年 ...
- API HOOK
API HOOK技术是一种用于改变API执行结果的技术,Microsoft 自身也在Windows操作系统里面使用了这个技术,如Windows兼容模式等. API HOOK 技术并不是计算机病毒专有技 ...
- POJ 3761 Bubble Sort(乘方取模)
点我看题目 题意 : 冒泡排序的原理众所周知,需要扫描很多遍.而现在是求1到n的各种排列中,需要扫描k遍就变为有序的数列的个数,结果模20100713,当然了,只要数列有序就扫描结束,不需要像真正的冒 ...
- 【转】Java自动装箱、拆箱、缓冲池
JDK5以后 Integer a = 3; 这是自动装箱int i = new Integer(2); 这是自动拆箱就是基本类型和其对应的包装类型在需要的时候可以互相 ...
- Ubuntu中安装DiscuzX2
http://blog.csdn.net/kevin_ysu/article/details/7452938 一.Apache的安装 Apache作为一个功能强大的Web程序,自然是架建Web服务器的 ...
- nginx负载均衡和反向代理有什么区别
近在研究nginx的负载均衡和反向代理,先看下这两个简单的配置吧! 负载均衡 worker_processes 1; events { worker_connections 1024; } http{ ...
- Qt之自定义控件(开关按钮)Qt之模拟时钟
http://blog.csdn.net/u011012932/article/details/52164289 http://blog.csdn.net/u011012932/article/det ...