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& ...
随机推荐
- How to install JDK (Java Development Kit) on Linux
This tutorial will guide you on how to install JDK (Java Development Kit) on Linux. Since I use Cent ...
- Android之ScaleGestureDetector(缩放手势检测)
一.概述 ScaleGestureDetector这个类是专门用来检测两个手指在屏幕上做缩放的手势用的,最简单的应用就是用来缩放图片或者缩放网页. 二.要求 利用ScaleGestureDetecto ...
- HDU 4948
题目大义: 给一张图,任意两点间有单向边,找出一种方案,使得每个新入队的点与队中的点距离<=2. 题解: 贪心,从最后入队点开始反向插入,每次找出最大入度的点入队. 只需证明最大入度点A与所有未 ...
- uva580Critical Mass
递推. 用f[i]代表i个盒子的放法,设g[i]=2^n-f[i],代表i个盒子不满足条件的放法. 枚举第一个U所在的位置j.则方法有g[j-2]*(2^(i-j-2))种,j-1必须是L. 所以 ...
- UVa 11584 Partitioning by Palindromes【DP】
题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...
- phonegap archive 报错 Cordova/CDVViewController.h' file not found
在BuildSettings->Header Search Paths 增加如下路径,问题解决 $(OBJROOT)/UninstalledProducts/include "$(O ...
- libogg.so fro android编译方法
在网站下载源代码http://www.xiph.org/downloads/ 解压然后打开cygwin配置 CXX=arm-linux-androideabi-g++.exe LD=arm-linux ...
- Eclipse “Invalid Project Description” when creating new project from existing source
1) File>Import>General>Existing Project into Workspace2) File>Import>Android>Exist ...
- Kafka 设计与原理详解
一.Kafka简介 本文综合了我之前写的kafka相关文章,可作为一个全面了解学习kafka的培训学习资料. 转载请注明出处 : 本文链接 1.1 背景历史 当今社会各种应用系统诸如商业.社交.搜索. ...
- Storm实战常见问题及解决方案
该文档为实实在在的原创文档,转载请注明: http://blog.sina.com.cn/s/blog_8c243ea30101k0k1.html 类型 详细 备注 该文档是群里几个朋友在storm实 ...