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;
}
#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;
}
相关文章推荐
- Android 键盘属性
- NoSQL 数据库
- 【GDOI2014模拟】Pty爬山
- 【转】apache/php 开启 gzip压缩
- GNU bash实现机制与源代码简析
- Kernel panic
- 大型网站架构系列:负载均衡详解
- setContentView(R.layout.xxx)出错解决方法
- zabbix简单入门
- HashMap线程不安全在哪里
- Maven仓库介绍
- 读《构造之法》8、9、10章有感
- 实战修复grub
- C#图片截取压缩(百分比压缩/大小压缩)实现代码
- 图片轮播——两个ImageView实现
- [django] Deploy Django Applications Using uWSGI and Nginx on Ubuntu 14.04
- Yii2框架使用计划任务的方法
- 出国(国际会议)-韩国
- 高级Bash脚本编程指南
- 面向对象 —— 类设计(九) —— 类设计的内在一致性