G. Mission in Amman (B)
There are K hours left before Agent Mahone leaves Amman! Hammouri doesn't like how things are going in the mission and he doesn't want to fail again. Some places have too many students covering them, while other places have only few students.
Whenever Hammouri commands a student to change his place, it takes the student exactly one hour to reach the new place. Hammouri waits until he is sure that the student has arrived to the new place before he issues a new command. Therefore, only one student can change his place in each hour.
Hammouri wants to command the students to change their places such that after Khours, the maximum number of students covering the same place is minimized.
Given the number of students covering each place, your task is to find the maximum number of students covering the same place after K hours, assuming that Hammouri correctly minimizes this number.
Input
The first line of input contains two integers M (2 ≤ M ≤ 105) and K (1 ≤ K ≤ 109), where M is the number of places and K is the number of hours left before Agent Mahone leaves Amman.
The second line contains M non-negative integers, each represents the number of students covering one of the places. Each place is covered by at most 109 students.
Output
Print the maximum number of students covering a place after K hours, assuming that Hammouri minimized this number as much as possible in the last K hours.
Examples
5 4
3 4 1 4 9
5
2 1000000000
1000000000 4
500000002
5 3
2 2 2 2 1
2
题目链接:http://codeforces.com/gym/100989/problem/G
题目大意:M个城市,K次移动。每次移动可以移动一个人,通过移动使得M个城市中人数最多的城市人数最少。
解题思路:通过二分查找,判断从人数的平均值到给出城市人数的最大值可以通过K次操作到达的最小值。
代码如下:
#include<stdio.h> long long ans; long long n,m; long long a[110000]; long long ave,sum=0; bool judge(long long mid) { long long cnt=0; for(long long i=1;i<=n;i++) { if(a[i]>mid) cnt+=a[i]-mid; } if(cnt<=m) return 1; else return 0; } void solve(long long l,long long r) { long long mid=(l+r)/2; while(l<=r) { if(judge(mid)) { ans=mid; r=mid-1; } else { l=mid+1; } mid=(l+r)/2; } printf("%lld ",ans); } int main() { long long maxx=-1; scanf("%lld%lld",&n,&m); for(long long i=1;i<=n;i++) { scanf("%lld",&a[i]); sum+=a[i]; if(a[i]>maxx) maxx=a[i]; } ave=sum/n; if(sum%n) ave++; solve(ave,maxx); return 0; }