It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.
The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k≤ 109).
Output a single integer — the minimal possible number of minutes required to dry all clothes.
sample input #1 3 2 3 9 5 sample input #2 3 2 3 6 5
sample output #1 3 sample output #2 2
题意:这道题是说衣服每分钟会自己蒸发掉一滴水,而烘干机每分钟会烘干m滴水,假设一件衣服被烘干,自己蒸发x分钟,烘干机烘干y分钟,那么这件衣服的水滴数应该等于x+y*m,只用二分法定的范围应该是0到水滴数最多的衣服蒸发干所需要的时间,而且不能使用cin输入,否则会超时,要用scanf
代码:
#include<iostream> #include<algorithm> #include<cmath> #include<cstdio> using namespace std; int a[100005]; int n; int m; int fun(int x) { int sum = 0; for(int i = 0;i<n;i++) { if(a[i]>x) sum+=(a[i]-x+m-2)/(m-1); if(sum>x) return 0; } return 1; } int main() { while(cin>>n) { for(int i = 0;i<n;i++) { scanf("%d",&a[i]); } cin>>m; sort(a,a+n); if(m==1) { cout<<a[n-1]<<endl; continue; } int small = 1; int big = a[n-1]; int ans = 0; while(small<=big) { int mid = (small+big)/2; if(fun(mid)) { ans = mid; big = mid-1; } else small = mid+1; } cout<<ans<<endl; } }