题意:有几件带水(ai)的衣服,每分钟自然烘干一个单位的水, 使用烘干机的话每分钟额外烘干k个单位的水,烘干机每次只能放一件,求烘干全部衣服的最少时间。
解题思路:二分查找最少的时间,可行性的判定比较高明, 一开始用的是a[i] - mid / k,无限WA,后来发现应该是a[i] - mid / k -1因为,假设这件衣服用烘干机烘了X分钟,那么另外的mid-X时间它都在自然晾干,减少的湿度为k * X+(mid-X)>=Wi,解得X>=(Wi-mid)/(k-1)所以, 又是神题啊。
注意不能除0。
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
Memory: 1088 KB | Time: 313 MS | |
Language: G++ | Result: Accepted |
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)
int buf[10];
inline long long read()
{
long long x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void writenum(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 100005
const int INF = 0x3f3f3f3f;
int a[MAX_N];
int n, k;
bool f(int x)
{
int cnt = 0;
int tmp = 0;
for(int i = n - 1 ; i >= 0 ; i--)
{
tmp = a[i] - x;
if(tmp > 0)
{
if(tmp % k)
{
cnt += (tmp / k) + 1;
}
else
{
cnt += tmp / k;
}
if(cnt > x) return false;
}
else
{
break;
}
}
return true;
}
int main()
{
while(~scanf("%d", &n))
{
for(int i = 0 ; i < n ; i++)
{
a[i] = read();
}
k = read() - 1;
if(k == 0)
{
printf("%d\n", *max_element(a, a + n));
continue;
}
sort(a, a + n);
int lb = -1, ub = INF;
while(ub - lb > 1)
{
int mid = (ub + lb) / 2;
if(f(mid)) ub = mid;
else lb = mid;
}
printf("%d\n", ub);
}
return 0;
}