POJ 2373 单调队列+dp

http://poj.org/problem?id=2373

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

Hint

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且L一定是偶数, 给出每个喷灌器的喷洒范围[A,B],一个喷灌器向两侧喷洒。 现给出n各区间, 要求每个区间只能被一个喷灌器喷洒。(一个点可以被多个喷灌器喷洒 如样例所示)你要在满足上述条件的基础上把区间[1,L]全部喷洒到,问所需要的最少喷灌器的个数,如果不能实现则输出-1。

思路:这题还挺有意思的,也让我对单调队列有了更深刻的认识。每个区间[S,E]只能被一个喷灌器喷洒,也就是说任意一个喷灌器的喷洒范围的右端点不能位于区间[S+1,E-1]内,因此我们考虑用vis数组来标记。我们用dp[i]表示喷洒[1,i]区间所需要的最少喷灌器个数,由上述分析知当vis[i]=1时,应该直接跳过这种情况。对于其他情况,我们有dp[i]=min(dp[j]+1),其中A<=(i-j)/2<=B。很容易想到暴力枚举,但是会超时,这里要用到单调队列的知识。从转移方程就可以看出来这是一个不减的函数,我们用单调队列q维护区间[i-2*B,i-2*A]内的哪些位置可以使使用的喷灌器个数最少,设队头元素为j,若(i-j)/2>B,则做出队操作;否则当队列非空时,我们知道当前的最优解一定是:dp[i]=dp[q[fon]]+1;我们还少一个入队的操作,怎么办呢?从转移方程可以发现,当一个位置j满足(i-j)/2>=A的时候,才能把它加入到单调队列q中,因此我们需要用另外一个队列来存储位置信息。至于单调队列的维护操作具体就看代码吧,其核心思想就是:若存在j、k满足j<k且value[k]<=value[j]时,一定可以用k替代j。(因为维护的是最小值 后来出现且值比前面的小 那前面的一定没用了)

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std ;

int q[1000005];//单调队列 维护 [i-2B,i-2A] 所需要的最少喷灌器的位置
int wait[1000005];//存储位置信息
int dp[1000005];
int vis[1000005];
int n,l,A,B,s,e;

int main()
{
    scanf("%d %d",&n,&l);
    scanf("%d %d",&A,&B);
    for(int i=0;i<n;i++)
    {
        scanf("%d %d",&s,&e);
        for(int j=s+1;j<=e-1;j++)
            vis[j]=1;//标记
    }
    memset(dp,-1,sizeof(dp));
    dp[0]=0;
    int fon1=1,bak1=1;//fon<=bak -> 队列为空
    int fon2=1,bak2=2;
    wait[fon2]=0;//第一个位置
    for(int i=2;i<=l;i+=2)
    {
        if(vis[i])//该情况不合题意
            continue;
        while(fon2<bak2&&(i-wait[fon2])/2>=A)//队列非空且该位置满足题意
        {
            while(fon1<bak1&&dp[q[bak1-1]]>=dp[wait[fon2]])//维护单调队列q
                --bak1;
            q[bak1++]=wait[fon2];//插到单调队列中
            ++fon2;//同时位置队列要做出队操作
        }
        while(fon1<bak1&&(i-q[fon1])/2>B)//出队列
            ++fon1;
        if(fon1<bak1)//单调队列非空
        {
            dp[i]=dp[q[fon1]]+1;//即该位置的状态存在
            wait[bak2++]=i;//才能把该位置放到待选队列中
        }
    }
    printf("%d\n",dp[l]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/89677810