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 K hours, 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
Input
5 43 4 1 4 9
Output
5
Input
2 10000000001000000000 4
Output
500000002
Input
5 32 2 2 2 1
Output
2---------------------------------------二分,注意边界刚开始,以平均值和最大值为边界,计算可移动的数目小于题目所给值即可
1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdio> 5 #include <iomanip> 6 #include <string> 7 #include <set> 8 #include <queue> 9 #include <stack> 10 #include <map> 11 #include <algorithm> 12 13 using namespace std; 14 15 typedef long long LL; 16 const int INF = 0x3f3f3f3f; 17 const int MAXN = 100005; 18 const int MOD = 1e9+7; 19 20 int main() 21 { 22 int n, i; 23 LL k, num[MAXN], mx = 0, sum = 0; 24 cin >> n >> k; 25 for(i = 0;i < n;++i) 26 { 27 cin >> num[i]; 28 mx = max(mx, num[i]); 29 sum += num[i]; 30 } 31 // int ave = mx / m; 32 // if(mx % m) 33 // ave++; 34 // 等同于下行码 35 LL ave = (sum + n - 1) / n; 36 LL l = ave, r = mx, mid, ans; 37 while(l <= r) 38 { 39 mid = (l + r) >> 1; 40 sum = 0; 41 for(i = 0;i < n;++i) 42 if(num[i] > mid) 43 sum += (num[i] - mid); 44 if(sum <= k) 45 { 46 ans = mid; 47 r = mid - 1; 48 } 49 else 50 l = mid + 1; 51 // cout << sum << endl; 52 } 53 cout << ans << endl; 54 return 0; 55 }
原文地址:https://www.cnblogs.com/shuizhidao/p/9280022.html