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 ( ≤ n ≤ ). The second line contains ai separated by spaces ( ≤ ai ≤ ). The third line contains k ( ≤ k ≤ ).
Output
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input # sample input #
Sample Output
sample output # sample output #
Source
Northeastern Europe 2005, Northern Subregion
晾衣服:n件衣服各含a_i水分,自然干一分钟一单位,放烘干机一分钟k单位,一次只能晒一件。求最短时间。
取C(mid) := 能在mid分钟内处理完,然后二分即可。
这里有两个很好玩的陷阱
①每分钟烘干k单位的水,于是我就想当然地除k向上取整了((a_i – mid) / k)。其实应该除以k-1,列个详细的算式:
设需要用x分钟的机器,那么自然风干需要mid – x分钟,x和mid需要满足:
k*x + (mid – x) >= a_i,即 x >= (a_i – mid) / (k – 1)。
②当k=1的时候,很显然会发生除零错误,需要特殊处理。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std;
#define N 100006
#define ll long long
ll n;
ll k;
ll a[N];
bool solve(ll mid){
ll minute=;
for(ll i=;i<n;i++){
if(a[i]>mid){
minute+=(ceil((a[i]-mid)*1.0/(k-)));
}
}
if(minute>mid) return false;
return true; }
int main()
{
int ac=;
while(scanf("%I64d",&n)==){ ll low=;
ll high=;
for(ll i=;i<n;i++){
scanf("%I64d",&a[i]);
high=max(high,a[i]);
}
scanf("%I64d",&k);
if(k==){
printf("%I64d\n",high);
continue;
}
ll ans;
while(low<high){ ll mid=(low+high)>>;
if(solve(mid)){
high=mid;
}
else{
low=mid+; }
} printf("%I64d\n",low);
}
return ;
}