HDU 4004 The Frog's Games(二分)

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次,求跳到对岸青蛙最少一次要跳多少。

因为数据量是10^9,直接找1s会T,(1s处理数据量为10^6~10^7,撑死10^8),所以二分求跳跃距离(时间复杂度为logN);

用dis数组存储两个石子间的距离,然后从两石子间最大距离~L二分,直到left=right;

直接二分时mid是偏左的,所以修改左端点时mid+1;

求以x距离跳跃时步数,外循环i控制跳到哪一个dis,内循环j控制跳几个dis,一旦x<dis之和就steps++;

最后记得将i设置到即将要跳的dis,并且步数+1(因为求出来的i是到第n-1个dis);


做了好久的题啊……看了题解并没有理解最后还是自己一步一步模拟过程才懂的;

独立思考很重要!队友给了我很多提示,但是自己不把这个过程想明白永远都做不出来!


学会了新姿势system("pause")。。简直是我这种喜欢分析过程人的福音。。


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=500010;
int a[N];
int dis[N];
int L,n,m;
int jump(int x){
	int steps=0,i=1,j=0,sum=0;	
	while(i<n){
		sum=0;
		for(j=i;j<=n;j++){
			sum=sum+dis[j];
			if(x<sum){
				steps++;	
				break;
			}
		}
		i=j;
	}
	return steps+1;
}
int main(){
	while(~scanf("%d %d %d",&L,&n,&m)){
		int maxdis=0;
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		sort(a+1,a+n+1);
		a[0]=0;a[n+1]=L;
		for(int i=1;i<=n+1;i++){
			dis[i]=a[i]-a[i-1];
			maxdis=max(dis[i],maxdis);
		}

	
		
		if(m==1){
			printf("%d\n",L);
			continue;
		}
		else if(m==n+1){
			printf("%d\n",maxdis);
			continue;
		}
		n=n+1;
	
		int left=maxdis,right=L;
		while(left<right){
	
			int mid=(left+right)/2;	
//			system("pause");
//			cout<<mid<<" "<<jump(mid)<<endl;
			if(jump(mid)<=m){
				right=mid;
			}
			else {
				left=mid+1;
			}
		}
		printf("%d\n",left);
	}
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值