Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18921 | Accepted: 4742 |
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 kwater, 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件衣服需要烘干 每件衣服的含水量为ai 只有一台烘干机,每次只能烘干一件衣服且一次至少使用1分钟求使所有衣服含水量为0的最少时间是多少 |
程序输入 |
•输入包含三行 表示衣服的数量
|
解题思路 |
要求最小的时间 问题转换: 最小值问题判定性问题 很明显,这个问题满足单调性的条件对时间X进行二分来解决! |
对于给定的时间X,依次判断每一件衣服i•如果ai<=X,则该衣服可以自然烘干 •否则说明需要烘干机, 如果所有衣服需要烘干机的时间总和<= X则说明时间X是可行的,否则说明不可行 |
#include <iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include <algorithm>
#define NUM100010
using namespacestd;
//这个题目是自然干是同时一起进行的,不过,烘干就不是一起了,但是烘干的减少量是自然干加上烘干的减少量;
intmain()
{
longlong N,K;//衣服的件数和烘干机1分钟减少的水量;
long longa[NUM];
intcaseNO =0;
while(~scanf("%lld",&N)){
caseNO++;
// cout <<"sample input #"<<caseNO<<endl;//这些不要,是题目的误导;
memset(a,0,sizeof(a));
for(inti =0;i<N;i++)
scanf("%lld",&a[i]);
cin>>K;//输入一分钟减少的水量;
longlong l = 0,r = *max_element(a, a+N);//找到最大的水量;自然也是最长的烘干时间,都是自然干,肯定是最长的时间
// cout <<r<<endl;
if(K==1)
{
// cout <<"sample output #"<<caseNO<<endl;//这些不要,是题目的误导;
cout<<r<<endl;continue;}
while(l<=r){
longlong totle =0;//记录总的时间(烘干时间,自然时间随着烘干的时候在进行中);
longlongmid = (l+r)/2;//利用二分先确定一个时间
for(inti =0;i<N;i++){//判断这个时间能不能将其晾干;
if(a[i]>mid)
totle+=ceil((a[i]-mid)*1.0/(K-1));//烘干时间上取整;注意这里是上取整;
}
if(totle<=mid) r= mid-1;//如果是烘的时间用的很少,说明我还能将最小的时间压缩在0~mid之间;
elsel = mid+1;//反之;
}
// cout <<"sample output #"<<caseNO<<endl;//这些不要,是题目的误导;
cout<<l<<endl;//最终,就是l这个值是最小的值;
}
return0;
}