码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3104 Drying (二分)

时间:2015-05-16 12:03:17      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:poj

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;
}


POJ 3104 Drying (二分)

标签:poj

原文地址:http://blog.csdn.net/u013068502/article/details/45766259

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!