POJ - 3258

River Hopscotch

Time limit : 2000 ms Memory limit : 65536 K

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


題目大意:
有個地方每年都會舉辦一個跳跳遊戲,讓一羣牛從河中的n個石塊依次跳躍到達指定終點。Farmer John爲了鍛鍊牛的跳躍力,打算將其中的m個石塊撤掉。求所有情況中 最大的 最小跳躍值 。

解題思路:
1.最大化最小值,採用二分的方法,對最小值進行二分搜索。每次判斷當前的搜索值的可行性。
2.在對一個mid值進行判斷的時候,兩塊相鄰石頭距離如果超過mid,就判斷下兩個。如果小於,前面的不動,找到最近的相距超過mid的石塊,累加中間跳過的石塊數。
3.一旦累加的石塊數超過了m就說明mid的值太大了。而如果累加完畢未超過m,則表示可行,但不一定是最優。
4.由於可能撤掉全部的石塊,初始的區間可設置爲 0 ~ 2*end。


源代碼

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<sstream>
using namespace std;

int ed, n, c;
int a[500005];

bool test(int mid) {
    int last = 0,num=0;
    int i = 0;
    while (i <= n) {
        if (a[i] - last < mid) {
            int cur = i + 1;
            while (cur<=n && a[cur] - last < mid)
                cur++;
            num += cur - i;
            if (num > c)
                return false;
            i = cur;
        }
        last = a[i];    //不要忘記修改上一個石塊的位置
        i++;

    }
    return true;
}


int main() {
    int i,res;
    while (~scanf("%d%d%d", &ed,&n,&c)) {
        int l=0, h=2*ed, m;
        for (i = 0; i < n; i++) {
            scanf("%d", &a[i]);
        }
        a[n] = ed;
        sort(a, a + n);
        while (h >= l) {
            m = (h + l) >> 1;
            if (test(m)) {
                res = m;
                l = m + 1;
            }
            else
                h = m - 1;
        }
        printf("%d\n", res);
    }
    return 0;
}
發佈了30 篇原創文章 · 獲贊 14 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章