POJ3104 Drying
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13703 | Accepted: 3527 |
Description
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.
Input
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
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input #1
3
2 3 9
5
sample input #2
3
2 3 6
5
Sample Output
sample output #1
3
sample output #2
2
Source
这是一道潮湿的题……
二分测试答案,看用这个时间能不能把所有衣服都烘干。
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=120000; 9 int n;long long k; 10 long long a[mxn]; 11 long long ans=0; 12 long long calc(long long x){ 13 int i,j; 14 long long cnt=0; 15 for(i=1;i<=n;i++){ 16 if(a[i]>x){ 17 long long xx = ceil((a[i]-x) * 1.0 / (k - 1) ); 18 cnt+=xx; 19 } 20 } 21 return cnt; 22 } 23 int main(){ 24 scanf("%d",&n); 25 int i,j; 26 long long mxt=0; 27 for(i=1;i<=n;i++) 28 scanf("%lld",&a[i]),mxt=max(mxt,a[i]); 29 scanf("%lld",&k); 30 if(k==1){ 31 printf("%lld\n",mxt); 32 return 0; 33 } 34 ans=0; 35 long long l=1,r=mxt; 36 while(l<=r){ 37 long long mid=(l+r)/2; 38 long long res=calc(mid); 39 if(res<=mid){ 40 ans=mid; 41 r=mid-1; 42 } 43 else l=mid+1; 44 } 45 printf("%lld\n",ans); 46 return 0; 47 }