POJ3258River Hopscotch (二分法的应用)

                                                     River Hopscotch

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, Lunits 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 D i from the start (0 < D i < 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
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

 题意:

当我看到这道题的时候,感觉跟A,B两道题(A,B题没看过的同学可以看一下我上一个博客,也是二分的题目,个人觉得解释的很清楚了)真的差不多,反正都是二分模板套套着用,然后稍微改一下就可以了。这道题的背景是牛在河中间跳跳比赛,人要过去把石头移掉,也就是求最小距离的最大值问题。

思路:

这道题的思路很简单,跟第A题基本一样,取得最大距离与最小距离,那么移除石头后的最短距离必然在之之间,那么我们可以再这个区间内对结果进行二分,看二分出的最短距离是否能符合移除m个石头的方案即可。

好,接下来我们直接上代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <cstring>
#include <string>
#include <list>
#include <algorithm>
#include <set>
#include <list>
//#include <map>
//#define INF 0x3f3f3f3f
 
//枚举最短距离,计算移除的石块数是否大于允许移除的数目
using namespace std;
 
long long int rock[50005];
int l, n, m;
 
int counting(int dis)
{
	int p = 0, counter = 0;
	for(int i=1;i<=n;i++)
	{
        //这里两个if的条件和牛栏那道题刚好相反
		if (rock[i] - p < dis&& i != n)
		{
            //此处尝试不加判断i是否为终点石头也AC了,若有dalao明白求解释
			counter++;
		}
		else p = rock[i];
	}
	return counter;
}
 
int removing()
{
	long long int min = 0, max = rock[n], mid;
	while(min<=max)
	{
		mid = (min + max) / 2;
		int removal = counting(mid);
		if(removal<=m)
		{
			min = mid + 1;
		}
		else if(removal>m)
		{
			max = mid - 1;
		}
	}
	return max;
}
 
int main()
{
	while(scanf("%d%d%d",&l,&n,&m)!=EOF)
	{
		int i;
		for (i = 1; i <= n; i++)
		{
			scanf("%d", &rock[i]);
		}
		n++;
        //加入提前输入的终点
		rock[n] = l;
		sort(rock + 1, rock + n + 1);
		printf("%d\n", removing());
	}
	return 0;
}
 

或者这样写也可以:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define w(a) while(a)
#define ll long long
 
int a[50005];
 
int main()
{
    int l,n,m,i,j,k,minn,maxn,mid,sum,cnt;
    w(~scanf("%d%d%d",&l,&n,&m))
    {
        minn=1000000005;
        maxn=l;
        a[0]=0,a[n+1]=l;
        up(i,1,n)
        scanf("%d",&a[i]);
        n++;
        sort(a,a+n);
        up(i,1,n)
        minn=min(minn,a[i]-a[i-1]);
        w(minn<=maxn)
        {
            mid=(maxn+minn)>>1;
            cnt=sum=0;
            up(i,1,n)
            {
                if((sum+=a[i]-a[i-1])<=mid)
                    cnt++;
                else//连续的几个石头距离和大于mid的话,再把连续距离清0重新枚举
                    sum=0;
            }
            if(cnt<=m)
            minn=mid+1;
            else
            maxn=mid-1;
        }
        printf("%d\n",minn);
 
    }
 
    return 0;
}

这两种写法都比较简单的,如果还没懂的同学,可以看一下我上两篇博客讲的二分法的问题,大家可以进行对比,比较着看。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值