【二分法】POJ3258-River Hopscotch

【抱大腿】啊啊啊又是一道恶心的题目!!!这道题是出在二分法里面的,因为这跟前面的一道青蛙过河的题特别像但是不一样,按照青蛙过河那个思路来走根本行不通,正好要按照跟那个思路相反的想法来想才行~



【题目】

River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
xxxxxxxxx马赛克xxxxxxxxx   xxxxxxxxx马赛克xxxxxxxxx

Description

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to Mrocks (0 ≤ M ≤ N).

FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set ofM rocks.

Input

Line 1: Three space-separated integers: LN, and M 
Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

Output

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

Sample Input

25 5 2
2
14
11
21
17

Sample Output

4

Hint

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

【题目解释】
有一堆牛要过河,河的长度是l,河中间有n个石头,牛只能踩着石头过河,问去掉m个石头后(去掉这m个石头的方式是随机的)的每种情况牛能走的石头间距最小值中,最大的那一个是多少(WTF!?)。
输入有多组实例,每组实例第一行输入l、n、m,下面n行输入石头距离起点的距离(起点为0)。

【解题思路】
这道题注意是要求一堆最小距离里面的最大值,逐个遍历绝逼要超时,要使用二分法。这道题里面注意牛能跳的距离是没有限制的,就是说牛想跳多远就跳多远,对于牛最多能跳多少次也没有限制!但是要求里面的最小值,所以这道题二分法所设置的中间值mid应为两石头间的最小距离并测试在此最小距离下,牛能否在跳到对岸的过程中,不存在比此mid距离更小的石头间距,以及跳完后,去掉的石头数 x(牛在跳跃并遇到小于mid距离的石头时,为了保证最小跳跃长度为mid,小于mid距离的石头不能往上面跳,因为如果一跳就会出现比mid更小的距离了,mid在这里做判断就没有意义了,这里去掉的石头数就是没有被牛跳上去的石头的石头数)跟要求的m数量相比,如果x>m时,说明牛每次跳的距离都太远了,即此时mid太大了,此时right变为mid来更新mid;如果x<m,那么说明牛也可以跳过m-x个石头,不踩就行了,要是x==m,那正好。但是对于每种x<=m的情况,其所出现的最小距离mid可能各不相同,找个变量把每次出来的mid值都比较一下取最大值就好啦~
这就是解题的大致思路。
那么……可能还会有同学有疑惑:“牛过河为毛要用跳的而且还必须要跳到石头上!?”


【代码】

#include<cstdio>
#include<algorithm>
using namespace std;

int a[50001]={0},l,n,m,i;
bool fun(int m);

int main()
{
    while(~scanf("%d%d%d",&l,&n,&m))
    {    
        int left=0,right=l,mid,ans;
        for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
        sort(a,a+(n+1));
        while(right>=left)
        {
            mid=(left+right)/2;//mid表示最小的距离! 
            if(fun(mid))
			{
            	left=mid+1;
            	ans=mid;
			}
			else
			right=mid-1;
        }
        printf("%d\n",ans);
    }
}

bool fun(int mid)
{
	int start=0,x=0;//用start表示每次落脚点的坐标,每落一次地更新一次start 
	for(i=1;i<=n;i++)
	{
		if(a[i]-start<mid)
		x++;//x表示去掉的石头数,如果mid大于要跳的距离,就跳过当前这个石头,此时x++ ,并且不落地 
		else
		start=a[i];//此时落地! 
	}
	if(l-start<mid)//判断最后一跳跳的距离要是小于mid的话那是不可以的 
	return false;
	if(x>m)//要是x>m就说明最小距离mid太大啦 
	return false;
	return true;
}



(参考了大神Rachel Zhang的代码)


发布了131 篇原创文章 · 获赞 16 · 访问量 1万+
展开阅读全文

River Hopscotch

10-25

Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L). To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river. Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N). FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks. Input Line 1: Three space-separated integers: L, N, and M Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position. Output Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks Sample Input 25 5 2 2 14 11 21 17 Sample Output 4 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览