POJ3104 Drying

题目

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

Source

Northeastern Europe 2005, Northern Subregion

题解

知识点:二分。

注意到可行性关于最小值的函数是单调的,答案在零点处,因此决定二分答案。

关于 judge 函数,只需要让大于 mid 的衣服吹风,而小于等于的不需要管。每件衣服吹风次数为 a[i]midk1=a[i]mid+k2k1 ,注意分母是 k1 ,非常坑,因为吹风时候不算自然晾干,因此吹风 k 要分一个 1 出来当作自然晾干的小于等于 mid 部分里,多出来的除以 k1 取上整。如果吹风次数超标就不可行,否则可行。

时间复杂度 O(n)

空间复杂度 O(n)

代码

#include <iostream>
#include <algorithm>
using namespace std;
int n, k;
int a[100007];
bool judge(int mid) {
long long cnt = 0;
for (int i = 0;i < n;i++) {
if (a[i] > mid) {
if (k == 1) return false;
cnt += (a[i] - mid + k - 2) / (k - 1);
}
}
if (cnt > mid) return false;
else return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0;i < n;i++) cin >> a[i];
cin >> k;
int l = 1, r = 1e9;
while (l <= r) {
int mid = l + r >> 1;
if (judge(mid)) r = mid - 1;
else l = mid + 1;
}
cout << l << '\n';
return 0;
}
posted @   空白菌  阅读(28)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示