`
hellojyj
  • 浏览: 58727 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

POJ 3258 River Hopscotch

    博客分类:
  • ACM
阅读更多

原题传送门:http://poj.org/problem?id=3258

 

 

River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6656   Accepted: 2880

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

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

Source

 
分析:二分搜索题,每次二分得到mid值的时候去判断下能不能扔掉M块石头。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 50010
#define t 20000000

int st[MAXN];
int flg[MAXN];
int flag[MAXN];
int l,n,m,mid;

bool isOK()
{
    int p,k;
    p = -1;
    for (int i=1;i<=n;i++)
    {
        while (st[i]-st[p+1]>=mid)
        {
            p++;
        }
        k=p<0?t:min(flg[p],flag[p]);
        flag[i] = k+i-p-1;
        flg[i] = min(flag[i-1],flg[i-1])+1;
    }
    if (flag[n]<=m)
    {
       return true;
    }else{
        return false;
    }


}

int main()
{
    scanf("%d%d%d",&l,&n,&m);
    for (int i=1;i<=n;i++){
        scanf("%d",st+i);
    }
    st[++n]=l;
    sort(st+1,st+n);
    int l=1,r=st[n];
    flg[0]=t; flag[0]=0;
    while (r>=l)
    {
        mid=l+r>>1;
        if (isOK())
        {
            l=mid+1;
        }
        else{
            r=mid-1;
        }
    }
    printf("%d\n",l-1);
    return 0;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics