HDU 4004 The Frog's Games

The Frog's Games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)


Problem Description
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they 
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
 

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.
 

Sample Input
  
  
6 1 2 2 25 3 3 11 2 18
 

Sample Output
  
  
4 11
 

先讲下大概题意,一只青蛙从河岸要跳到对岸去,河的宽度是L,两岸间有n块石头排成一列相当于桥,青蛙的每一次跳跃必须落在石头上或者是岸上,最多只能跳m次,题目给出从出发点(岸上)到n块石头的距离,要求输出青蛙在满足上述要求的条件下要到达对岸,最少的跳跃能力是多远,即青蛙以每次跳ans远的距离跳跃,最后需要跳x次才能到达对岸,那么我们要尽量地使x接近于m且x<=m,求出此时的ans值。

先求出两相邻石头之间的距离的最大值MAX,MAX是青蛙在跳跃次数无限制的情况下的最小跳跃能力(如果这个都不满足,青蛙根本都过不了河),然后青蛙的能力也不用太强,最小跳跃能力最多是L,因此以[MAX,L+1)(也可以是[MAX,L],个人习惯使用左闭右开区间)作为二分搜索的区间,根据具有的能力计算出所需跳跃次数TIMES和m比较,不断二分缩小搜索范围,最后找到一个最小的能力值使TIMES<=m且TIMES接近于m。


#include <stdio.h>
#include <algorithm>
#define MAX 500000
using namespace std;

int dis[MAX+5];
int L,n,m;

int Step(int ablt){
	int i,cnt,now;
	i=cnt=now=0;
	while(now<L){
		now+=ablt;
		while(dis[i+1]<=now)
			i++;
		now=dis[i];
		cnt++;
	}
	return cnt;
}

int BinSearch(int low,int high){
	int mid;
	while(low<high){
		mid=low+((high-low)>>1);
		if(Step(mid)<=m)
			high=mid;
		else
			low=mid+1;
	}
	return low;
}

void scani(int &num){
	char ch;
	int flag;
	while(ch=getchar(),ch!='-'&&(ch<'0'||ch>'9'));
	if(ch=='-')
		flag=-1,num=0;
	else
		flag=1,num=ch-48;
	while(ch=getchar(),ch>='0'&&ch<='9')
		num=num*10+ch-48;
	num*=flag;
}

int main(){
	int i,ans;
	dis[0]=0;
	while(~scanf("%d",&L)){
		scani(n),scani(m);
		for(i=1;i<=n;i++)
			scani(dis[i]);
		sort(dis+1,dis+1+n);
		dis[n+1]=L,dis[n+2]=(L<<1)+1;
		ans=dis[1];
		for(i=2;i<=n+1;i++)
			if(dis[i]-dis[i-1]>ans)
				ans=dis[i]-dis[i-1];
		ans=BinSearch(ans,L+1);
		printf("%d\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值