您的位置:首页 > 其它

poj 3104 二分枚举答案,最值问题转化为判定性问题

2016-05-25 12:44 316 查看
点击打开链接http://poj.org/problem?id=3104

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#define M 101000
using namespace std;
__int64 a[M],n,k;
bool check(__int64 time) // 贪心->判定时间 x是否可行
{ // 把洗衣机留给之x时间内不能自然晾干的用 算出每件衣服最少要用多少分钟洗衣机
int i; //判断用洗衣机时间之和是否小于等于总时间
__int64 ans=0;
for(i=0;i<n;i++)
{
if(a[i]<=time) continue; // 可以自然晾干
else
{
ans+=ceil((a[i]-time)*1.0/(k-1)); // x1用洗衣机时间 x2自然晾干
if(ans>time) return 0; // kx1+x2>=a[i]
//x1+x2=time
} //联立 x1>= (a[i]-time)/(k-1)
} // x1 应取最小 有小数应向上取整
return 1;
}
int main()
{
__int64 mid,ans,r,l;
int i,t;
while(scanf("%I64d",&n)!=EOF)
{
r=0;
for(i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
if(r<a[i]) //最多要多长时间 (全部自然晾干)
{
r=a[i];
}
}
scanf("%I64d",&k);
if(k==1)
{
printf("%I64d\n",r); //洗衣机也是1的话 最短时间即为r
continue;
}
l=1;;
while(l<=r) // 最小值问题转化为判定性问题
{
mid=(l+r)/2;
t=check(mid);
if(!t)
{
l=mid+1;
}
else
{
ans=mid;
r=mid-1;
}
}
printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: