POJ 3104 Drying(二分答案)
2015-09-07 09:15
288 查看
Drying
TimeLimit:2000MS | MemoryLimit:65536K | |
TotalSubmissions:11128 | Accepted:2865 |
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; }
相关文章推荐
- gcc 参数详解[转载]
- django中给ajax提交加上csrf
- CLR via C# 提纲
- eclipse 快捷键
- AVL树讲解
- 如何在Spring MVC中自定义注解
- xxx can not access a member of class xxxx with modifiers ""
- Java设计模式 -- 适配器模式
- 苹果通知推送服务(APNS)一些关键特性摘要
- 配置rsync服务器
- 国内VDI市场之乱象分析,第二篇:传输协议篇
- c 强大的vim配置文件,让编程更随意
- MongoDB学习四
- JavaScript常用标签和方法总结
- 前端模块化--这是我看过讲得比较好的东东
- 设置Chrome忽略网站证书错误
- html 表格小demo 1
- msp430 看门狗
- java静态方法是否能重写问题
- 源码推荐(9.07):Quartz纯代码绘制hellokit机器猫,滚动图片选取HySideScrollingImage