您的位置:首页 > 其它

POJ 3104 Drying(二分答案)

2015-09-07 09:15 288 查看
题目链接:http://poj.org/problem?id=3104
Drying

TimeLimit:2000MSMemoryLimit:65536K
TotalSubmissions:11128Accepted:2865
Description

Itisveryhardtowashandespeciallytodryclothesinwinter.ButJaneisaverysmartgirl.Sheisnotafraidofthisboringprocess.Janehasdecidedtousearadiatortomakedryingfaster.Buttheradiatorissmall,soitcanholdonlyonethingatatime.

Janewantstoperformdryingintheminimalpossibletime.Sheaskedyoutowriteaprogramthatwillcalculatetheminimaltimeforagivensetofclothes.

TherearenclothesJanehasjustwashed.Eachofthemtookaiwaterduringwashing.Everyminutetheamountofwatercontainedineachthingdecreasesbyone(ofcourse,onlyifthethingisnotcompletelydryyet).Whenamountofwatercontainedbecomeszerotheclothbecomesdryandisreadytobepacked.

EveryminuteJanecanselectonethingtodryontheradiator.Theradiatorisveryhot,sotheamountofwaterinthisthingdecreasesbykthisminute(butnotlessthanzero—ifthethingcontainslessthankwater,theresultingamountofwaterwillbezero).

Thetaskistominimizethetotaltimeofdryingbymeansofusingtheradiatoreffectively.Thedryingprocessendswhenalltheclothesaredry.

SampleInput

sampleinput#1
3
239
5

sampleinput#2
3
236
5

SampleOutput

sampleoutput#1
3

sampleoutput#2
2

题意:有n件湿衣服,现在要把它们烘干(或自然风干),已知烘干机的效率是每分钟k的水分。自然风干是每分钟1的水分。
求最短的烘干时间(整数)。
分析:0时间内一定不能烘干。MAX(水分最多的自然风干用时)的时间一定能烘干(因为不用烘干,也能风干)假设用时mid则:
如果a[i]<=mid自然风干就行啦,如果a[i]>mid就要用到烘干机,假设用x分钟的烘干机,则需要满足a[i]-k*x<=mid-x
得出x=ceil(a[i]-mid)/(k-1)【其中ceil是向上取整的函数,包含在cmath头文件里】得出各个衣服所需要的烘干时间x,
然后加起来等于res如果res<mid则减小mid(用二分法),否则增大mid。直到求出正确答案。细节详见代码!

二分答案方法的要求:
《1》能够确定出答案所在的范围。
《2》答案有在范围内的单调性
《3》答案是整数(或有一定的精度要求),而不是十分精确的实数。
二分答案的时间复杂度是logN。


#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
usingnamespacestd;

intn,a[100005],k;
boolcheck(intmid)
{
longlongres=0;
for(inti=0;i<n;i++)
{
if(a[i]>mid)
{
res+=(int)ceil((double)(a[i]-mid)/(k-1));
}
}
returnres<=mid;
}

intmain()
{
while(scanf("%d",&n)!=EOF)
{
intMax=0;
for(inti=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>Max)Max=a[i];
}
scanf("%d",&k);
if(k==1)
{
printf("%d\n",Max);
continue;
}
intleft=0,right=Max,mid;
while(right>=left)
{
mid=(right+left)>>1;
if(check(mid))right=mid-1;
elseleft=mid+1;
}
printf("%d\n",left);
}
return0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: