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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值