Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D
有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差距有多少。
先sort一下,最富的人很明显不会低于sum(a1 ~ an) / n , 所以二分一下最富人的最小值 看是否满足操作k。
最穷的人不会高于sum(a1 ~ an) / n + 1 ,所以二分一下最穷人的最大值 看是否满足操作k。
(注意一点的是二分以后最穷的人的最大值和最富的人的最小值要是相同,那么要讨论一下总和是否被n整除的情况,要是整除那么差距就为0,否则就为1。)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + ;
typedef long long LL;
LL a[MAXN], n;
//最穷人的的最大值
bool check_min(LL x, LL k) {
LL cnt = upper_bound(a + , a + n + , x) - a;
for(int i = ; i <= cnt - ; ++i) {
k -= (x - a[i]);
if(k < )
return false;
}
return true;
} bool check_max(LL x , LL k) {
LL cnt = upper_bound(a + , a + n + , x) - a;
for(int i = cnt; i <= n; ++i) {
k -= (a[i] - x);
if(k < )
return false;
}
return true;
} int main()
{
LL k , sum = ;
scanf("%lld %lld", &n, &k);
for(int i = ; i <= n; ++i) {
scanf("%lld", a + i);
sum += a[i];
}
sort(a + , a + n + );
if(a[] == a[n]) {
printf("0\n");
return ;
}
LL l = a[], r = sum / n + , L = a[], R = a[n];
while(l < r) { //最穷人的的最大值
LL mid = (l + r) >> ;
if(check_min(mid, k)) {
l = mid + ;
L = mid;
}
else
r = mid;
}
l = sum / n, r = a[n];
while(l < r) {
LL mid = (l + r) >> ;
if(check_max(mid, k)) {
r = mid;
R = r;
}
else
l = mid + ;
}
printf("%lld\n", R > L? R - L: (sum % n ? : ));
}
Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)的更多相关文章
- 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 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 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& ...
随机推荐
- js 中 typeof 的使用
js中的变量是松散类型(即弱类型)的,可以用来保存任何类型的数据. typeof 可以用来检测给定变量的数据类型,可能的返回值: 'undefined' --- 这个值未定义 'boolean' -- ...
- 查看Linux服务器网络状态
ifconfig 用来显示所有网络接口的详细情况的,如:ip地址,子网掩码等. ethx是以太网网卡的名称. 配置文件在/etc/sysconfig/network-scripts/ifcfg-eth ...
- hdu 4939 Stupid Tower Defense ( dp )
题目链接 题意:给出一条长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后的格子每秒造成y点伤害, 蓝塔可以使通过单位长度的时间增加z秒 ...
- hdu 4864 Task (贪心 技巧)
题目链接 一道很有技巧的贪心题目. 题意:有n个机器,m个任务.每个机器至多能完成一个任务.对于每个机器,有一个最大运行时间xi和等级yi, 对于每个任务,也有一个运行时间xj和等级yj.只有当xi& ...
- hdu4576 概率dp n^2的矩阵
这个题目看网上好多题解都是直接O(n*m)卡过.我是这么做的. 对于m次操作,统计每个w的次数.然后对每个w做矩阵乘法. 这样直接做矩阵乘法是会TLE的. 又由于这里的矩阵很特殊,一次乘法可以降维成O ...
- 类Item_equal
class Item_equal: public Item_bool_func { List<Item_field> fields; /* list of equal field item ...
- BZOJ2298: [HAOI2011]problem a
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2298 题解:刚开始思考的方向错了...一直在想LIS什么的,又发现不合法的情况不好判断,真是个 ...
- RGB图像数据字符叠加,图像压缩(ijl库),YUV转RGB
jackyhwei 发布于 2010-01-01 12:02 点击:3218次 来自:CSDN.NET 一些非常有用的图像格式转换及使用的源代码,包括RGB图像数据字符叠加,图像压缩(ijl库),Y ...
- angular+rails集成实战
http://start.jcolemorrison.com/setting-up-an-angularjs-and-rails-4-1-project/ 1. 添加gemgem 'sprockets ...
- POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11192 Accepted: 5492 ...