题目:
http://poj.org/problem?id=3104
题意:
有n件衣服, 每件衣服有ai滴水, 衣服每分钟干1滴水, 用烘干机则干k滴水,但是烘干机每次只能放入一件衣服. 求所有衣服干的最少用时.
思路:
二分搜索. 关键的是在时间允许的范围内,使得每件衣服使用烘干机的时间最少,目的是为其他衣服节省烘干机的时间.
k*t + (x - t) >= ai. 则 t >= (ai - x) / (k - 1).
k = 1时,会出现除零现象,要特判.
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
int n, k;
ll clo[100005];
bool can(ll x)
{
ll time = 0;
for(int i = 0; i < n; ++i) {
if(clo[i] <= x) continue;
ll mor = clo[i] - x;
ll cnt = mor / (k - 1);
if(mor - cnt * (k - 1) > 0) cnt++;
time += cnt;
if(time > x) return false;
}
return true;
}
void solve(ll maxn)
{
ll l = 0, r = maxn;
while(r - l > 1) {
ll mid = (r + l) / 2;
if(can(mid)) r = mid;
else l = mid;
//printf("%d %d\n", l, r);
}
printf("%I64d\n", r);
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
ll maxn = 0;
for(int i = 0; i < n; ++i) {
scanf("%I64d", &clo[i]);
maxn = max(maxn, clo[i]);
}
scanf("%d", &k);
if(k == 1) {
printf("%I64d\n", maxn);
continue;
}
solve(maxn);
}
return 0;
}