HDU_4004_The frog's game_二分搜索

今天广州站亚洲区域赛热身,他们都去当志愿者了,我没衣服


题意:

跳过宽度为l的河,河中有n个石头,直线排列,坐标已知,设跳跃一次最远为x,要求跳m次以下,问x最小是多少



Input
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
 

Output
For each case, output a integer standing for the frog's ability at least they should have.


水题二分搜索即可,每次尝试能否跳过去,跳过去需要多少次跳跃

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define mxn 501000 
int a[mxn];
int L, n, m;
int judge( int x ){
	int cnt = 0;
	int pos = 0;
	for( int i = 1; i <= n + 1; ++i ){
	/*	if(a[i] - a[pos] > x){
			if(i-1==pos)	return 0;
			pos=i-1;
			++cnt;
			--i;
		}*/
		if( a[i] - a[i-1] > x )
			return 0;
		if( a[i] - a[pos] > x ){
			cnt++;
			pos = i - 1;
		}
		if( i == n )
			cnt++;
	}
	return cnt <= m;
}
int main()
{
	while( scanf("%d%d%d",&L,&n,&m) != EOF ){
		a[0] = 0;
		for( int i = 1; i <= n; ++i )
			scanf("%d",&a[i]);
		a[n+1] = L;
		sort( a, a + n + 1 );
		int LL = 0;
		int RR = L + 1;
		int ans = L + 1;
		while( LL + 1 < RR ){
			int m = ( LL + RR ) >> 1;
		//	cout<<"ll "<<LL<<" m "<<m<<" RR "<<RR<<endl;
			if( judge( m ) ){
				RR = m;
				ans = m;
			}
			else	LL = m;
		}
		printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值