poj 2373 Dividing the Path(dp+单调队列优化)

poj 2373 Dividing the Path(dp+单调队列优化)
总时间限制: 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.

来源
USACO 2004 December Gold

首先要能弄明白这是一个dp,但是光光明白这是一个dp不够,为了不TLE,还要优化。
这里用单调队列优化,这在程序设计实习教学ppt中有,算法我不是很懂,也不知道单调队列的用法,但是对这个题本身,先看一下以下代码把题啃下来吧,以后也许会明白的。以下为实现:
由此,可以在递推计算f(X)的过程中,维护一个队列qState。每次计算f(X)之前,若X-2A位于任何奶牛的活动范围之外,则执行下列操作:将X-2A插入队列qState的末尾 ,从队列qState中剔除属于{Y: Y< X-2B}的元素,若f(Y)>=f(X-2A) ,则将 Y从队列qState中删除。 则队列qState具有下列特征 :qState是一个 优先队列:若元素 X1排在X2之后,则f(X1)>f(X2),若f(X)存在,则f(X)=1+f(Y),其中Y是qState中的某个元素qState[qRead],Y >=X-2B,qState[qRead-1]< X-2B。

单调队列以后要学习

#define MaxL 1000001

#include<stdio.h>

int n,L,r,R,cow[MaxL],f[MaxL],queue[MaxL],head,tail;

int main()
{
    int i, s, e, cows;
    scanf("%d%d%d%d",&n,&L,&r,&R);
    for (i=0; i<=L; i++)  {
        f[i]=MaxL;
    }
    for (i=0; i<n; i++) {
        scanf("%d%d",&s,&e);
        for (int j=s+1;j<e;j++)
            cow[j]=1;
    }

    f[0]=0;

    for (i=2; i<=L; i+=2) {
        if (i<2*r)
            continue;
        while (head<tail && queue[head]<i-2*R)
            head++;
        while (head<tail && f[queue[tail-1]]>=f[i-2*r])
            tail--;
        queue[tail++]=i-2*r;
        if (f[queue[head]]!=MaxL && !cow[i])
            f[i]=f[queue[head]]+1;
    }
    if (f[L]==MaxL) f[L]=-1;
    printf("%d\n",f[L]);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值