Drying
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20586 Accepted: 5186
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
解题心得:
- 题意就是有n件衣服,每件衣服有一个湿润度,有一个烘干机,每次可以烘干一件衣服,每分钟烘干k的水分,每分钟可以选择放入/拿出一件衣服,如果衣服没有放入烘干机那么衣服每分钟会自然蒸发1水分。问怎么才能将所有衣服烘干并且所花时间要最少,最少时间是多少。
- 其实难点是如果不放入烘干机中衣服每分钟会自然蒸发1水分,那么有没有办法将自然蒸发这个条件去掉呢,其实还是可以的,我们可以假设用了m分钟,那么自然烘干的水分就是每件衣服m的水分,剩下的水分就是使用烘干机烘干的。但是每次枚举m分钟肯定会超时,那么就可以用到二分了,如果枚举出来的时间可以将衣服全烘干,那么就可以缩小时间,如果不能就扩大时间,二分得到最后的答案。
#include <algorithm>
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
int n,k,water[maxn];
void init() {
for(int i=0;i<n;i++)
scanf("%d",&water[i]);
scanf("%d",&k);
}
bool checke(ll time) {
ll min_time = 0;
for(int i=0;i<n;i++) {
if(water[i] <= time)
continue;
else {
int temp = water[i] - time;
min_time += temp/(k-1);
if(temp % (k-1))
min_time++;
}
}
if(min_time <= time)
return true;
return false;
}
ll binary_search() {
ll l,r;
l = 0, r = 1e9+100;
while(r - l > 1) {
ll mid = (l+r) >> 1;
if(checke(mid))
r = mid;
else
l = mid;
}
return r;
}
int main() {
while(scanf("%d",&n) != EOF) {
init();
if(k == 1) {
int Max = -1;
for(int i=0;i<n;i++)
Max = max(Max,water[i]);
printf("%d
",Max);
continue;
}
ll ans = binary_search();
printf("%lld
",ans);
}
return 0;
}