POJ-3104 Drying

题目链接

https://vjudge.net/problem/POJ-3104

题面

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

题意

有n件衣服,第i件含水分a[i],有一个烘干衣服的机器,把衣服放在其中每分钟会蒸干k的水分,在空气中每分钟蒸干1水分,问最短多少分钟可以蒸干所有衣服。

题解

首先比较容易看出来这是道二分答案的题,关键在于如何判断(二分答案的关键都在于如何判断),我们知道一共需要多少分钟(设为x)烘干后,那么所有所含水分小于等于x的我们可以不管,对于大于x的衣服,设它在空气中烘干a分钟,在机器中烘干b分钟,那么

\[\begin{equation} \left\{ \begin{array}{l} a+b=x\\ a+bk \ge a[i]\\ \end{array} \right. \end{equation} \]

可以推出\(b \ge \frac{(a[i]-x)}{k - 1}\),k=1时特判一下直接输出a[n]即可,在空气中的时间不会更新总时长,所以每件衣服必须要在烘衣机中呆的时间就会对答案产生贡献,所以我们要让b尽量小,所以b最小就是不等式右边向上取整,扫一遍判断总时长是否大于了二分的数即可。

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define N 100050
using namespace std;
typedef long long ll;
ll a[N];
int n, k;
bool check(ll x) {
	ll tmp = 0;
	for (int i = 1; i <= n; i++) {
		if (a[i] <= x) continue;
		tmp += ceil((double)(a[i] - x) / (k - 1));
	}
	return tmp <= x;
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	sort (a + 1, a + n + 1);
	scanf("%d", &k);
	if (k == 1) {
		cout << a[n] << endl;
		return 0;
	}
	ll l = 0, r = (ll)1e15;
	ll mid, ans = r;
	check(2);
	while (l <= r) {
		mid = (l + r) >> 1;
		if (check(mid)) {
			r = mid - 1;
			ans = min(ans, mid);
		}
		else l = mid + 1;
	}
	cout << ans << endl;
	return 0;
}

posted @ 2019-02-14 16:51  Artoriax  阅读(223)  评论(0编辑  收藏  举报