1. 程式人生 > >POJ-2373-Dividing the Path

POJ-2373-Dividing the Path

Description
Farmer John’s cows have discovered that the clover growing along the ridge of the hill in his field is particularly good. To keep the clover watered, Farmer John is installing water sprinklers along the ridge of the hill.

To make installation easier, each sprinkler head must be installed along the ridge of the hill (which we can think of as a one-dimensional number line of length L (1 <= L <= 1,000,000); L is even).

Each sprinkler waters the ground along the ridge for some distance in both directions. Each spray radius is an integer in the range A..B (1 <= A <= B <= 1000). Farmer John needs to water the entire ridge in a manner that covers each location on the ridge by exactly one sprinkler head. Furthermore, FJ will not water past the end of the ridge in either direction.

Each of Farmer John’s N (1 <= N <= 1000) cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval (S,E). Each of the cow’s preferred ranges must be watered by a single sprinkler, which might or might not spray beyond the given range.

Find the minimum number of sprinklers required to water the entire ridge without overlap.

Input
* Line 1: Two space-separated integers: N and L

  • Line 2: Two space-separated integers: A and B

  • Lines 3..N+2: Each line contains two integers, S and E (0 <= S < E <= L) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge and so are in the range 0..L.

Output
* Line 1: The minimum number of sprinklers required. If it is not possible to design a sprinkler head configuration for Farmer John, output -1.

Sample Input

2 8
1 2
6 7
3 6

Sample Output

3

解題思路:

從線段的起點向終點安裝噴水頭,令f(X)表示:所安裝噴水頭的噴灑範圍
恰好覆蓋直線上的區間[0 X]時,最少需要多少個噴水頭
顯然, X應滿足下列條件:
- X為偶數
- X所在位置不會出現奶牛,即X不屬於任何一個(S,E)
- X>=2A
- 當X>2B時,存在Y ⊆ [X-2B X-2A]且Y滿足上述三個條件,使得
f(X)=f(Y)+1

f(X)=1+min{f(Y): Y⊆[X-2B X-2A]、 Y位於任何奶牛的活動範圍之
外}: X>2B
- 對每個X求f(X),都要遍歷區間 [X-2B, X -2A]去尋找其中最小的f(Y),則時間複雜度為: L * B = 1000000 * 1000,太慢
- 快速找到[X-2B X-2A]中使得f(Y)最小的元素是問題求解速度的關鍵 。
可以使用優先佇列priority_queue!
- 求F(X)時,若座標屬於[X-2B, X-2A]的二元組(i,F(i))都儲存在一個priority_queue中,並根據F(i)值排序,則隊頭的元素就能確保是F(i)值最小的。
- 在求 X點的F(x)時,必須確保佇列中包含所有屬於 [X-2B,X-2A]的點。而且, 不允許出現座標大於X-2A的點,因為這樣的點對求F(X)無用,如果這樣的點出現在隊頭,因其對求後續點的F值有用,故不能拋棄之,於是演算法就無法繼續了。
- 佇列中可以出現座標小於 X-2B 的點。這樣的點若出現在隊頭,則直接將其拋棄。
- 求出X點的F值後,將(X-2A+2, F(X-2A+2))放入佇列,為求F(X+2)作準備佇列裡只要存座標為偶數的點即可

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

const int MAXL = 1000005;

struct Spr{
    int len;
    int num;
    Spr(int lenS = 0, int numS = 0) : len(lenS), num(numS) {
    }
    bool operator < (const Spr& sprS) const/*不可省略*/ {
        return num > sprS.num; //num越小越優先 
    }
}; // 用c++裡面的重構載函式構造優先佇列, 
priority_queue<Spr> queSpr;
int cow[MAXL];
int dp[MAXL];
int main()
{
    int n, l, ra, rb, start, end;
    while (~scanf("%d%d", &n, &l)) {
        scanf("%d%d", &ra, &rb);
        ra <<= 1; rb <<= 1;
        memset(cow, 0, sizeof(cow));
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &start, &end);
            cow[start + 1]++;
            cow[end]--;
        }
        int cowIn = 0;
        for (int i = 0; i <= l; i++) {
            cowIn += cow[i];//確定牛的活動範圍
            cow[i] = cowIn > 0;
        }
        for (int i = ra; i <= rb; i += 2) {
            if (!cow[i]) {
                dp[i] = 1;
                if (i <= rb + 2 - ra) {
                    queSpr.push(Spr(i, 1));
                }
            }
        }

        for (int i = rb + 2; i <= l; i += 2) {
            if (!cow[i]) {
                Spr sprS;
                while (!queSpr.empty()) {
                    sprS = queSpr.top();
                    if (i - rb > sprS.len) {
                        queSpr.pop();
                    }
                    else {
                        break;
                    }
                }
                if (!queSpr.empty()) {
                    dp[i] = sprS.num + 1;
                }   
            }
            if (dp[i + 2 - ra] != 0) {
                queSpr.push(Spr(i + 2 - ra, dp[i + 2 - ra]));
            } 
        }
        if (dp[l] == 0) {
            printf("-1\n");
        }
        else {
            printf("%d\n", dp[l]);
        }
    }
    return 0;
}