Codeforces 672D Robin Hood
来源:互联网 发布:java sftp上传文件 编辑:程序博客网 时间:2024/06/05 00:11
题目链接:http://codeforces.com/problemset/problem/672/D
题意:对一个序列操作k次,每次将当前最大的数中分出去1给最小的数(如果有多个最大或最小随机选择,不过不影响答案)。求最后最大的数和最小的数的差值。
思路:k<=1e9 所以我们可以用二分法来做。现在我们假设操作k次后,最小的数为x。那么所有小于x的数都要加到x,t1 = ∑(x-a[i]) ( a[i] < x ),k至少要是这么多才可以;再考虑上界,如果操作很多,会把所有的x变成x+1或者更大,这样最小值就不是x了。所以我们记录所有小于等于x的数量t2,也就是说,当前最少操作t1次,这样所有小于x的数都会变成x,然后我们还可以最多再操作t2-1次,使得当前最小数还是x(再操作t2次,最小的数就会变成x+1)。二分求解最大的数同理。
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod %100000007const int maxn = 500009;LL n,k;LL a[maxn];void init(){ cin>>n>>k; rep(i,1,n) scanf("%I64d",&a[i]);}int check( LL x , int type ){ LL t1 = 0;LL t2 = 0;rep(i,1,n)if ( type == 0 ) { if ( a[i] <= x ) { t1 += x - a[i]; t2++; } } else { if ( a[i] >= x ) { t1 += a[i] - x; t2++; } }t2+=t1;if ( t1 > k ) return -1;if ( k >= t2 ) return 1;return 0;}void solve(){ LL S = 0; LL m1,m2; LL m,l,r; LL ml,mr; LL temp;ml = mr = a[1]; rep(i,1,n) { S += a[i]; if ( a[i] < ml ) ml = a[i]; if ( a[i] > mr ) mr = a[i]; } m1 = m2 = -1; l = ml; r = S/n; while( l < r ) { m = ( l + r )>>1; temp = check(m , 0);if (temp == -1) r = m - 1;else if ( temp == 1 ) l = m + 1;else{m1 = m;break;} } if (m1==-1) m1 = l; l = (S+n-1)/n; r = mr; while( l < r ) { m = ( l + r ) >> 1; temp = check(m , 1);if (temp == -1) l = m + 1;else if ( temp == 1 ) r = m - 1;else{m2 = m;break;} } if (m2==-1) m2 = l; printf("%I64d\n",m2-m1);}int main(){ init(); solve(); return 0;}
0 0
- Codeforces 672D Robin Hood
- codeforces 672D. Robin Hood(暴力)
- Codeforces 672D Robin Hood (二分)
- codeforces 672D Robin Hood (二分)
- CodeForces 672D Robin Hood(二分)
- 【15.93%】【codeforces 672D】Robin Hood
- CodeForces 672D Robin Hood(二分)
- codeforces 672D D. Robin Hood (二分+思维)
- CodeForces - 672D Robin Hood (二分)好题
- Codeforces 672D Robin Hood 二分判定+思维
- CodeForces 672D Robin Hood(二分 + 思维)
- cf 672d Robin Hood 【二分】
- Codeforces 672D Robin Hood【思维+二分】这题思路有点劲啊
- Codeforces Round #352 (Div. 2) D. Robin Hood
- Codeforces #352 Div2 D Robin Hood(二分查找)
- Codeforces Round #352 (Div. 2) D Robin Hood : 模拟
- codefores 352 D. Robin Hood
- CodeForces 671B Robin Hood
- Python知识点系统图
- 2 cat 命令
- 在javaweb中的jquery ajax中的traditional
- android 固定图片长度和宽度的方法
- 多线程学习(六)线程的调度-让步&合并&守护线程
- Codeforces 672D Robin Hood
- C/C++中关键字extren作用总结
- Android Service进阶教程
- Sublime Text3使用技巧
- 非常全面的浏览器useragent判断代码
- Mac下SVN账号删除更换终极解决方法,百度都是坑爹的
- 用python修改haproxy配置文件
- Android_通过Intent调用系统相机和相册回传bitmap
- 十. Linux中bash shell的使用