Drying POJ - 3104(二分+思维)

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
题意:晾衣服,每分钟可以晾干1单位的水。用烘干机的话可以每分钟烘干k单位的水,但是一次只能烘干一件(烘干的时候忽略风干的影响),问最小多少分钟可以烘干所有的衣服。
思想:对于一件衣服,完全风干需要a[i]的时间。我们二分去枚举一个答案x,对于水分小于等于x的值,我们就不用管了。对于大于x的衣服,可以分为两部分,一部分晾衣服,一部分烘干衣服。那么一定存在一个值m,使得烘干的水量为(m-c)+ck的和最小。其中m为烘干衣服所用的时间,c为烘干的时间。也就是说现在要求的是 a[i]=(m-c)+ck -> c=(a[i]-m)*1.0/(k-1);我们求出所有衣服烘干时间的总和,判断是否大于答案x就行。详细解释看代码
代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;

const int maxx=1e5+100;
ll a[maxx],b[maxx],k;
int n;

bool judge(ll x)//x为烘干所有衣服的时间,y为这件衣服使用烘干机的时间。那么(x-y)+y*k=a[i]。那么使用烘干机的时间为y=(a[i]-x)/(k-1)。求出所有衣服使用烘干机的时间,不要大于x就好了。 
{
	for(int i=1;i<=n;i++) b[i]=a[i]-x;
	ll cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(b[i]>0)
		{
			cnt+=b[i]%(k-1)?b[i]/(k-1)+1:b[i]/(k-1);
		}
	}
	if(cnt<=x) return 1;
	return 0;
}
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		scanf("%lld",&k);
		sort(a+1,a+1+n);
		if(k==1)
		{
			printf("%lld\n",a[n]);
			continue;
		}
		ll l=1,r=a[n],mid,ans=0;
		while(l<=r)
		{
			mid=l+r>>1;
			if(judge(mid))
			{
				ans=mid;
				r=mid-1;
			}
			else l=mid+1;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值