`
fuerbosi
  • 浏览: 464671 次
文章分类
社区版块
存档分类
最新评论

HDU 4004

 
阅读更多

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4004


二分。不会。

看了题解才写出的。首先,青蛙最低必须满足的跳跃米范围为(ans=任意相邻两个石头的最大距离),青蛙的的跳跃范围为【ans,L】,然后对跳跃范围进行二分。

对于每一次的跳跃,有点贪心的思想,每次跳跃到哪个石子的选择,就是选择 离当前跳跃到的位置最近的石子。


下面是AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
int a[500005];
int l,n,m;
int solve(int step){
	int s=0,count=0,i=0;

	while(s<l){
		s+=step;

		while(a[i]<=s)
			i++;
		
		s=a[i-1];
		count++;

	
	}
	return count;

}
int binary_search(int low,int high){
	int left=low,right=high,mid;

	while(left<right){
		mid=(left+right)/2;
		if(solve(mid)<=m){
			right=mid;
		}
		else{
			left=mid+1;
		}
	}

	return left;

}
int main(){

	while(cin>>l>>n>>m){
		int ans,i;
		

		for(i=0;i<n;i++)// cin>>a[i];     用cin会超时。比赛尽量用scanf.
			scanf("%d",&a[i]);
		sort(a,a+n);
		a[n]=l; a[n+1]=l*2;
        ans=a[0];

		for(i=1;i<=n;i++)
			if(a[i]-a[i-1]>ans)
				ans=a[i]-a[i-1];

		ans=binary_search(ans,l);

		cout<<ans<<endl;


	}
	return 0;

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics