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& ...
随机推荐
- RAPIDXML 中文手册,根据官方文档完整翻译!
简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...
- 信息:Could not publish server configuration for Tomcat v6.0 Server at localhost. Multiple Context
需要把server.xml更正一下,去掉重复的context.或者把整个server文件夹都删掉,重新添加服务器.也可以在server窗口中删除server,再新添加一个server.
- parseInt和valueOf
.parseInt和valueOf.split static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行分析. static Integer valueOf( ...
- UVa 1640 (计数) The Counting Problem
题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ...
- ASP.NET缓存OutputCache和Response.Cache之C#后台设置
一.ASPX页面缓存页面缓存的使用方法非常的简单,只需要在aspx页的顶部加一句声明<%@ OutputCache Duration="60" VaryByParam=&qu ...
- noip2006提高组题解
第一题:能量项链 区间型动态规划 据说这题在当年坑了很多人. f(i, j) 表示从第i个珠子开始合并j个珠子所释放的最大能量. f(i, j) = max{ f(i, k} + f(i+k, j-k ...
- 3732 Ahui Writes Word
// N个物品 放进容量为C的背包里面 要求价值最大// 一看 第一反应是0 1背包 不过 N=100000 C=10000// 注意到 v,c在 10以内// 那么 最多就100种组合了 然后就转化 ...
- liunx环境下的mysql数据库配置文件my.conf内的参数含义
[client]port = 3306socket = /tmp/mysql.sock [mysqld]port = 3306socket = /tmp/mysql.sock basedir = /u ...
- Android02--debug.keystore的注册信息
1 -- 签名文件的密钥 默认签名文件的密码是:android 该文件的存放点是: 2 -- 签名文件的签名信息 keytool -list -v -keystore C:\Users\motadou ...
- 顶 企业站常用css横向导航菜单
<!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/T ...