POJ3258 River Hopscotch

http://poj.org/problem?id=3258
http://noi.openjudge.cn/ch0111/10/ “openjudge”

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

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).


题解: 实际和猜数字一样,在1-100以内有一个数字是结果数字,那么,你要猜测这个数字,你一定先猜50,然后对方说大了,你就以1-50的范围再去猜,你一定会猜25,对方说小了,你就以25-50的范围去猜。这就是二分,时间复杂度是log2级别的。
本题也是一样,你可以先设一个最大的最短跳跃距离mid逐一判断,如果两个石头之间的距离已经大于mid,继续向右扫,如果小于,则要去掉这个石头。

举例:
0 20 23 26 29 31 51 (r=52)

这样我们设1-50内有一个最小距离。

mid为(1+50)/2=25,则会移除20、23两块石头,再移除29、31两块石头,ans>m,r=mid=25
(这里思考,石头移除的多了什么意思?表示距离设的大了。因此我们要从1-25内的距离重新选择一个)
mid=(1+25)/2=13,则移除23、26、29、31,同理r=mid=13
mid=(0+13)/2=6,移除23,移除29,31,ans==m,l=mid=6
(这时可以再试着放大距离)
mid=(6+13)/2=9,移除23、26、31。ans==m了,就行了吗?不,明显20与29距离是9,还可以更长为11,那就加长试试,l=9
mid=(9+13)/2=11,移除23、26、29。此时还不能确定11就是最优解,因为还有12,所以l=11
mid=12,移除了23、26、29、31,所以r=12
mid=11,又区间是[l,r)左闭右开型的,即现在能确定距离就是mid=l=11了。


#include<iostream>
#include<cstdio>
using namespace std;
int l,r,s,n,m,river[50050],mid,ans;
int main()
{
    //cin>>s>>n>>m;
    scanf("%d%d%d",&s,&n,&m);
    for(int i=1;i<=n;i++)
//            cin>>river[i];
            scanf("%d",&river[i]);
    river[n+1]=s;
    river[0]=0;
    l=1;
    r=s-1;
    while(l<r) //
    {
        mid=(r+l)/2;
        if(mid==l)break; //说明加不长了,因为r还不变,如果l依然不变,则进入死循环 
        ans=0;
        int i=0;//初始化 
        int j=1;//初始化 
        while(j<=n+1) //这里就限制了j的范围 
        {
              while((river[j]-river[i])<mid && j<=n+1)            
                    j++;
              ans+=j-i-1;                      
              i=j;
              j++;
        }
        if(ans<=m)  l=mid;//加长试试 
        else r=mid;
    }
    cout<<mid<<endl;//或者l,比如10 11 (10+11)/2=10 mid==l 
    //while(1);
    return 0;
}

  1. 部分内容引用http://www.cnblogs.com/KakagouLT/p/5001800.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值