River Hopscotch(二分搜索)

River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6051   Accepted: 2616

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 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:  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(1 ~ 10 ^ 9),N(0 ~ 50000),M (0 ~ N)。代表有一条长为 L 的河,后给出 N 颗石头,需要从0开始跳跃到 L 长度的石头上,现要使间距最小值最大化,最多删除 M 颗石头,使其能跳到 L 石头上。

 

      思路:

      二分搜索。使最小值最大化。记得判断区间的时候是左闭右开的,所以 rb = l + 1,判断退出条件是 rb - lb <= 1。

      当这个距离所要删除的石头数 > m,说明距离太大,应该往左边搜,所以应该搜 [ l , mid );

      当这个距离所要删除的石头数 < m,说明距离太小,应该往右边搜,所以应该搜 [ mid , r );

      当这个距离所要删除的石头数 == m,说明距离刚好,但是有可能有更大的距离适合,应该找的是最后一个满足条件的距离,所以应该往右边搜,即 [ mid , r );

      最后输出值应该输出 l ,而不是 r ,因为这是 左闭右开 的区间。

 

      AC:

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

int l, n, m;
int rock[50005];

bool C(int d) {
        int del = 0, last = 0;

        for (int i = 1; i < n + 2; ++i) {
                if (rock[i] - rock[last] < d) {
                        ++del;
                } else last = i;
        }

        if (del <= m) return true;
        return false;
}

void solve() {
        int lb = 0, rb = l + 1;

        while (rb - lb > 1) {
                int mid = lb + (rb - lb) / 2;
                if(C(mid)) lb = mid;
                else rb = mid;
        }

        printf("%d\n",lb);
}

int main() {
        //freopen("test.in","r",stdin);

        scanf("%d%d%d", &l, &n, &m);

        rock[0] = 0;
        for (int i = 1; i <= n; ++i)
                scanf("%d", &rock[i]);
        rock[n + 1] = l;

        sort(rock,rock + n + 2);

        solve();

        return 0;
}

 

 

 

你可能感兴趣的:(搜索)