POJ 3104 Drying (二分)

本文介绍了一个关于衣物快速干燥的问题及解决方法。通过使用二分搜索技术寻找最优干燥时间,实现衣物在有限资源条件下最短时间内的完全干燥。
Drying
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10098 Accepted: 2589

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件衣服,含水量为ai,如果用烘衣机烘的话就是每分钟衣服含水量少k,而不烘的可以风干,那么每分钟含水量减少1,但烘的时候是不考虑风干的那1含水量的,并且烘衣机每次只能烘一件衣服,含水量为0说明衣服干了。问最少用多少分钟把衣服全部弄干。

二分答案。

对于C(x)一开始想不出来,就知道是ai<=x的就不用管它,直接风干就行,重点去烘那么ai>x的衣服,关键那么多衣服该怎么烘,顺序是什么,没件衣服该烘多长时间,明显这里不需要考虑顺序,只需对所有的时间求和再和x比就对了。
假设第i件衣服需要烘的话,设烘X分钟,风干Y分钟,每分钟烘掉k,得:
X+Y=x; X*k+Y>=ai
得X>=(ai-x)/(k-1) ,对这个式子向上取整,然后对所有>x的ai求出X的和与x比较即可。

明显k=1时要特殊处理。
a/b向上取整的技巧:(a+b-1)/b

/*
二分求下界
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef __int64 ll;

const int maxn=100000+10;
ll a[maxn];
ll n,k;

bool C(ll x){
	ll i,cnt=0;
	for(i=0;i<n;i++)
		if(a[i]>x)
			cnt+=(a[i]-x+k-2)/(k-1);
	return cnt<=x;
}
	

int main()
{
	ll i,j,max;
	while(scanf("%d",&n)!=EOF){
		max=-1;
		for(i=0;i<n;i++){
			scanf("%I64d",&a[i]);
			if(max<a[i])
				max=a[i];
		}
		scanf("%I64d",&k);
		if(k==1){
			printf("%I64d\n",max);
			continue;
		}
		ll l=0,r=1000000000;
		while(r-l>1){
			int m=(l+r)/2;
			if(C(m))
				r=m;
			else
				l=m;
		}
		printf("%I64d\n",r);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值