题目链接:http://poj.org/problem?id=3104
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18378 | Accepted: 4637 |
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 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e5+10; 19 20 int n, k, a[MAXN]; 21 22 bool test(int mid) 23 { 24 int time = 0, left = mid; 25 for(int i = n; i>=1; i--) 26 { 27 a[i] -= time; // a[i]的值已经发生改变了!!!!!! 应该另开一个数组用于操作,以保证原始数值不被修改 28 if(a[i]<=left) continue; 29 int t = (a[i]-left)/(k-1); 30 if((a[i]-left)%(k-1)) t++; 31 time += t; 32 left -= t; 33 if(left<0) return false; 34 } 35 return true; 36 } 37 38 int main() 39 { 40 while(scanf("%d",&n)!=EOF) 41 { 42 for(int i = 1; i<=n; i++) 43 scanf("%d", &a[i]); 44 scanf("%d", &k); 45 sort(a+1, a+1+n); 46 if(k==1) 47 { 48 printf("%d ", a[n]); 49 continue; 50 } 51 52 int l = 1, r = a[n]; 53 while(l<=r) 54 { 55 int mid = (l+r)>>1; 56 if(test(mid)) 57 r = mid - 1; 58 else 59 l = mid + 1; 60 } 61 printf("%d ", l); 62 } 63 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e5+10; 19 20 int n, k, a[MAXN]; 21 int tmp[MAXN]; 22 23 bool test(int mid) //模拟过程 24 { 25 //time为当前时间, left为剩余时间, time与left之和恒为mid 26 int time = 0, left = mid; 27 memcpy(tmp, a, sizeof(tmp)); //!!!!!! 28 for(int i = n; i>=1; i--) 29 { 30 tmp[i] -= time; //过了多长时间, 就风干了多少水。 31 if(tmp[i]<=left) continue; //如果剩下的时间可以让这件衣服完全风干, 则跳过;否则下一步 32 int t = (tmp[i]-left)/(k-1); //t为使得这件衣服可以风干的最少烘干次数 33 if((tmp[i]-left)%(k-1)) t++; //向上取整 34 time += t; //更新当前时间和剩余时间 35 left -= t; 36 if(left<0) return false; //如果超额,则不能满足条件 37 } 38 return true; 39 } 40 41 int main() 42 { 43 while(scanf("%d",&n)!=EOF) 44 { 45 for(int i = 1; i<=n; i++) 46 scanf("%d", &a[i]); 47 scanf("%d", &k); 48 sort(a+1, a+1+n); 49 if(k==1) //当k为1时, 烘干机没用了, 全部自己风干。 50 { 51 printf("%d ", a[n]); 52 continue; 53 } 54 55 int l = 1, r = a[n]; 56 while(l<=r) 57 { 58 int mid = (l+r)>>1; 59 if(test(mid)) 60 r = mid - 1; 61 else 62 l = mid + 1; 63 } 64 printf("%d ", l); 65 } 66 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e5+10; 19 20 int n, k, a[MAXN]; 21 22 bool test(LL limit) 23 { 24 int sum = 0; 25 for(int i = n; i>=1; i--) 26 { 27 if(a[i]<=limit) continue; //直接风干 28 int t = (a[i]-limit)/(k-1); //t为使得这件衣服可以风干的最少烘干次数 29 if((a[i]-limit)%(k-1)) t++; //向上取整 30 sum += t; //时间累加 31 if(sum>limit) return false; //超过时间限制, 退出 32 } 33 return true; //满足条件 34 } 35 36 int main() 37 { 38 while(scanf("%d",&n)!=EOF) 39 { 40 for(int i = 1; i<=n; i++) 41 scanf("%d", &a[i]); 42 scanf("%d", &k); 43 sort(a+1, a+1+n); 44 if(k==1) //当k为1时, 烘干机没用了, 全部自己风干。 45 { 46 printf("%d ", a[n]); 47 continue; 48 } 49 50 int l = 1, r = a[n]; 51 while(l<=r) 52 { 53 int mid = (l+r)>>1; 54 if(test(mid)) 55 r = mid - 1; 56 else 57 l = mid + 1; 58 } 59 printf("%d ", l); 60 } 61 }