poj 2373:Dividing the Path 灌溉草场(优先队列问题)

总时间限制:
1000ms
内存限制:
65536kB

描述
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.
输入
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.
输出
* Line 1: The minimum number of sprinklers required. If it is not possible to design a sprinkler head configuration for Farmer John, output -1.
样例输入

2 8
1 2
6 7
3 6

样例输出

3

提示
INPUT DETAILS:

Two cows along a ridge of length 8. Sprinkler heads are available in integer spray radii in the range 1..2 (i.e., 1 or 2). One cow likes the range 3-6, and the other likes the range 6-7.

OUTPUT DETAILS:

Three sprinklers are required: one at 1 with spray distance 1, and one at 4 with spray distance 2, and one at 7 with spray distance 1. The second sprinkler waters all the clover of the range like by the second cow (3-6). The last sprinkler waters all the clover of the range liked by the first cow (6-7). Here’s a diagram:

                 |-----c2----|-c1|       cows' preferred ranges
     |---1---|-------2-------|---3---|   sprinklers
     +---+---+---+---+---+---+---+---+
     0   1   2   3   4   5   6   7   8

The sprinklers are not considered to be overlapping at 2 and 6.

这一篇的意思就是说,有个草场,是长度为 L 的,然后,有n头牛在这个线上吃草,每个牛有一个活动范围(s,e)(不要问为啥草场可以抽象成一条线,牛可以抽象成一个点,我也不知道)然后,要安装若干个喷水头,每个喷水头的范围是[a,b],要求最少安装多少个喷头。。。

由题意可以得:1、长度L是偶数,因为如果是奇数,那么喷水头必定会喷出范围。2、题目里面表示,每个牛只能在一个喷水头的覆盖下面跑,就是说,有牛的地方,不能有喷水头覆盖的最末端。

这道题用动态规划+优先队列(关于啥是优先队列,后面科普),递推公式是:f(x) = min(f(g)) {g in [x-2b, x-2a]}+1。。。就是说,要求在L的位置的最小值,只要求出L前面那个喷水头允许喷到的范围[x-2b,x-2a]中(每个数字都代表了一个喷头能够喷到的地方),最小使用了多少个喷头可以喷到,然后在加1就是到L能够喷到的最小数字了。

如果是枚举出所有的情况,必定超时,因此,用优先队列优化,代码如下:

#include <iostream>
#include <cstring>
using namespace std;
#define MAXL 1000010
#define MAXN 1010
#define inf 0x3f3f3f3f
int dp[MAXL], p[MAXN];
int n, l, a, b, head, tail;
void in(int t)
{
    //更新一下头指针
    while(head<tail && p[head]<t-2*(b-a)) head++;
    //更新一下尾指针,这里加不加等号都可以,区别是,如果加了等号,就如果遇到相同的点也会从队列里删除了
    while(head<tail && dp[p[tail-1]]>=dp[t]) tail--;
    tail++;
    //记录当前的点
    p[tail-1] = t;
}
int dpro(){
    head=tail=0;
    dp[0] = 0;
    for (int i = 1; i < 2*a; ++i) {
        dp[i] = inf+1;
    }
    for (int i = 2*a; i <= l; i+=2) {
        // 添加点2*a到这个队列中,这里只能为偶数,
        // 因为每次加进去的点其实是这个喷头能够喷到的最大范围,这个范围必定是偶数
        in(i-2*a);
        if(dp[i]<=inf){
            dp[i] = dp[p[head]]+1;
        }
    }
    if(dp[l]>=inf) return -1;
    else return dp[l];
}
int main()
{
    int s, e, cows=0, cow[MAXL];
    cin>>n>>l>>a>>b;
    memset(dp, inf, sizeof(dp));
    memset(cow, 0, sizeof(cow));
    //这里用的技巧是,用一个数组保存一下从那个点开始有奶牛,从哪个点开始没有奶牛,
    // 然后把这个数字加上dp里面的数字,在更新dp时进行判断即可
    for (int i = 0; i < n; ++i) {
        cin>>s>>e;
        cow[s+1]++;
        cow[e]--;
    }
    for (int i = 0; i < l; ++i) {
        cows += cow[i];
        dp[i] += cows;
    }
    if(l&1==1) cout<<"-1"<<endl;//如果是奇数,则返回-1
    else cout<<dpro()<<endl;
}

这里写图片描述

java 版

import java.util.*;
public class Main {
    public static int n,l,a,b;
    public static final int inf = 0x3f3f3f3f; // 定义一个无穷大
    public static final int maxl = 1000010; // 最大线段长度
    public static int [] cows = new int[maxl];
    public static int [] dp = new int[maxl];
    public static int [] p = new int[maxl];
    public static int head;
    public static int tail;
    public static int size;

    public static void in(int t){
        while(head<tail && p[head]<(t-size)) head++;
        while(head<tail && dp[p[tail-1]]>=dp[t]) tail--;
        tail++;//为了保证这个尾元素一定要指向下一个队列位置
        p[tail-1] = t;
    }

    public static int dpro(){
        head = 0;
        tail = 0;
        dp[0] = 0;
        for (int i = 1; i < 2*a; i++) {
            dp[i] = inf+1;
        }
        for (int i = 2*a; i <= l; i+=2) {
            //在队列中加入点i-2*a
            in(i-2*a);
            if(dp[i]<=inf){
                dp[i]=dp[p[head]]+1;
            }
        }
        if (dp[l]>=inf) {
            return -1;
        }
        else return dp[l];
    }

    public static void main(String args[]) {
        int cow=0;
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        l = s.nextInt();
        a = s.nextInt();
        b = s.nextInt();
        size = 2*(b-a);
        //初始化原来的数组
        for (int i = 0; i < l; i++) {
            dp[i] = inf;
            cows[i] = 0;
        }
        //根据奶牛重新对这个dp数组赋值
        for (int i = 0; i < n; i++) {
            int ss, ee;
            ss = s.nextInt();
            ee = s.nextInt();
            cows[ss+1]++;
            cows[ee]--;
        }
        for (int i = 0; i < l; i++) {
            cow += cows[i];
            dp[i] += cow;
        }
        if((l&1)==1) System.out.println("-1");
        else System.out.println(dpro());
    }
}

这里写图片描述

以上解题思路来自《算法基础与在线实践》一书和网上一篇博文,但是那篇博文找不到了。。。。ORZ。。。。总之感谢各位分享啦。。。

单调队列有下面性质:

1、从队首到对尾,是严格单调曾或者减的

2、从对首到对尾,是按照时间从早到晚的,最早如队的,在队首。

在这道题里,相当于每次求一个滑动窗口范围里的最小值,只要比较加入的新加入的那个值和队尾元素的大小即可。如果比对尾小,则去掉原来的对尾,继续比较,如果比对尾大,则加在对尾后面。。。更新分为更新队首和对尾两部分,更新队首是为了维护对列的元素在指定范围里面。。。。。

具体实现,大家可以参考这篇文件中的内容,感谢分享:https://www.cnblogs.com/iiyiyi/p/4662835.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值