cf 671/B Robin Hood(二分)@
2017-05-22 11:52
330 查看
Robin Hood
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins.
Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person
right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and
want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number
of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among
richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) —
the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th
of them is ci (1 ≤ ci ≤ 109) —
initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
input
output
input
output
Note
Lets look at how wealth changes through day in the first sample.
[1, 1, 4, 2]
[2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
题意:n个人,每个人ci的金币,每天最富有的人都会给最贫穷的人1金币,问k天后最富有人和最贫穷的人差了多少金币。
解:二分枚举最小值得最大值,和最大值的最小值,因为二者都是在K的允许范围内操作,所以一定可以中和,如果最小值大于了最大值,那么就是相遇情况考虑综合是否能被N整除
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins.
Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person
right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and
want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number
of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among
richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) —
the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th
of them is ci (1 ≤ ci ≤ 109) —
initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
input
4 1 1 1 4 2
output
2
input
3 1
2 2 2
output
0
Note
Lets look at how wealth changes through day in the first sample.
[1, 1, 4, 2]
[2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
题意:n个人,每个人ci的金币,每天最富有的人都会给最贫穷的人1金币,问k天后最富有人和最贫穷的人差了多少金币。
解:二分枚举最小值得最大值,和最大值的最小值,因为二者都是在K的允许范围内操作,所以一定可以中和,如果最小值大于了最大值,那么就是相遇情况考虑综合是否能被N整除
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<vector> #include<cmath> #include <bits/stdc++.h> using namespace std; const int N = 1e6+7; typedef long long LL; const int inf = 0x3f3f3f3f; char str[1000]; LL a , b ; int cmp(LL A,LL B) { return A>B; } int n; LL k, k2; int judge1(LL x) { int i; LL sum=0, sum1=0; for(i=0;i<n;i++) { if(a[i]<x) sum+=(x-a[i]); else break; if(sum>k) return 0; } if(sum>k) return 0; return 1; } int judge2(LL x) { LL sum=0, sum1=0; int i; for(i=0;i<n;i++) { if(b[i]>x) sum+=(b[i]-x); else break; if(sum>k) return 0; } if(sum>k) return 0; return 1; } int main() { LL sum=0; scanf("%d %I64d", &n, &k); for(int i=0;i<n;i++) { scanf("%I64d", &a[i]); b[i]=a[i], sum+=a[i]; } sort(a,a+n); sort(b,b+n,cmp); if(k==0) { cout<<b[0]-a[0]<<endl; return 0; } LL l=a[0], r=sum, ans1, ans2; while(l<=r) { LL mid=(l+r)/2; if(judge1(mid)) l=mid+1,ans1=mid; else r=mid-1; } l=a[0], r=sum; while(l<=r) { LL mid=(l+r)/2; if(judge2(mid)) r=mid-1,ans2=mid; else l=mid+1; } if(ans2>ans1) cout<<ans2-ans1<<endl; else printf("%d\n",sum%n!=0); return 0; }
相关文章推荐
- codeforces 671B /Round #352(div.2) D.Robin Hood 二分
- CF 6E Exposition(RMQ | 线段树,二分)
- CF_318_Div.2 Bear and Elections(二分||暴力枚举)
- CF 208E Blood Cousins(二分+DFS)
- CF 808D D. Array Division(二分)
- CF 551c 二分搜索+思维题
- CF - 813B. The Golden Age - 数学+暴力+二分
- CF 8D Two Friends 【二分+三分】
- Codeforces Round #352 (Div. 1) B. Robin Hood(想法+二分,好题)
- CF 779D String Game 二分,贪心匹配字符串
- CF-#402D String Game(二分+优先队列)
- cf Two Sets (我用二分最大匹配做的)
- Codeforces Round #352 (Div. 1) B. Robin Hood (以后二分就这么写吧)
- CF_479D Long Jumps(二分)
- 【Codeforces Round 339 (Div 2)D】【暴力 贪心 二分】Skills 最高技能人数乘cm+最低技能乘cf权值最大
- CF 二分
- 【打CF,学算法——二星级】CodeForces 626C Block Towers(暴力/二分)
- 【打CF,学算法——三星级】CodeForces 567D One-Dimensional Battle Ships (二分)
- cf 551 C(二分)
- cf 689 C(二分)