hdu 4004
来源:互联网 发布:网络工作 编辑:程序博客网 时间:2024/06/01 07:18
转自:
http://blog.csdn.net/non_cease/article/details/6769814
题目大意:
见题目描述;
基本思路:
二分跳的距离;然后从头扫到尾,n*logn的时间复杂度;(注意,细节处理十分重要可以视为一种技巧
题目描述:
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 500000+10;
int stone[maxn];
int length,number,times;
bool judge(int x)
{
if(x*times<length) return false;
int i=0,cnt=0;
for(int j=1;j<=number+1;j++)
{
if(stone[j]-stone[i]>x)
{
if(j==i+1) return false;
else
{
i=--j;//**************非常重要,找到跳的石头编号后,立即让j退回来比i大一个编号,防止出现相邻距离的差值大于x却漏判的情况;
cnt++;
}
}
}
if((++cnt)>times) return false;
else return true;
}
int main()
{
while(scanf("%d%d%d",&length,&number,×)==3)
{
stone[0]=0;//好技巧,把两岸的情况变为数组元素,方便判断,以后类似的题目学着这么做;
for(int i=1;i<=number;i++)
{
scanf("%d",&stone[i]);
}
stone[number+1]=length;
sort(stone,stone+number+2);
int left=0,right=length+1,middle;
while(right>left)
{
middle=left+(right-left)/2;
if(judge(middle)) right=middle;
else left=middle+1;
}
printf("%d\n",left);
}
return 0;
}
- HDU 4004
- hdu 4004
- HDU 4004
- hdu 4004
- hdu 4004
- HDU 4004
- HDU 4004
- HDU 4004
- hdu 4004
- hdu 4004
- 【二分】hdu 4004
- hdu 4004 二分法
- hdu 4004 二分 过河
- hdu 4004题解
- 二分查找 hdu 4004
- hdu 4004 最大值最小化
- hdu 4004(二分)
- hdu 4004 4006 4010
- Android AOP之字节码插桩
- 浅谈md5加密
- 实现生产者-消费者模式
- 最大子数组II
- 【11.7】c++ primer plus 课后编程答案
- hdu 4004
- Java实现-最大子数组1
- mysql 主键自增 但是报主键重复的异常问题探究- Duplicate entry
- AlertDialog创建6种对话框的用法 (对话框,单选对话框,多选对话框,列表对话框,自定义对话框,退出对话框)
- Error setting expression 'orderid' with value ['2', ]
- 阿里云远程连接服务器oracle数据库
- Android 自定义View基础 onMeasure & onLayout
- 字符串的反转
- python和js