Drying
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25550 | Accepted: 6395 |
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
题意解析:
Jane洗完了自己的衣服,然后需要把这些衣服给弄干,给出一个 n(衣服的个数),第二行给出 n 个数,分别是衣服上面水分的数量,第三行给出 k,也就是烘干机一秒烘的水分数量,如果不用烘干机,会自动每秒蒸发 1 单位的水分;
总烘干时间 t ∈ [ 0 , maxT ] , maxT = max { a [ i ] };
方法就是使用二分的思想,去二分答案,分时间,看每次mid时间内,衣服能不能全部弄干,不能就更新mid;
代码篇:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N_MAX = 100000 +4;
int N;
LL a[N_MAX], k, aa[N_MAX];//k代表用吹风机后衣服每次失水量
bool C(LL d) //判断用d天能否将所有衣服弄干
{
memcpy(aa,a,sizeof(a));
for (int i = 0; i < N; i++)
{
aa[i] -= d;
if (aa[i] < 0)
aa[i] = 0;
}
if (k == 1) !!!!!!!!!!!!!!!!!!!!k=1会RE
{
LL M = *max_element(aa,aa+N);
if (M == 0)
return true;
else
return false;
}
LL num = 0;
for (int i = 0; i < N; i++)
{
if (aa[i] > 0)
{
if (aa[i] % (k - 1))
num += (aa[i] / (k - 1) + 1);
else
num += aa[i] / (k - 1);
}
if (num > d)
return false;
}
return true;
}
int main()
{
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%lld", &a[i]);
}
scanf("%lld",&k);
// LL lb = *min_element(a, a + N) / k, ub = *max_element(a, a + N);
LL lb = 0, ub = *max_element(a, a + N);
while (ub - lb > 1)
{
LL mid = (lb + ub) / 2;
if (C(mid))
ub = mid;//说明mid天可以全干,也许还可以用更少的天数
else
lb = mid;
}
printf("%lld\n", ub);
return 0;
}
OVER!