POJ2373Dividing the Path 单调队列+DP

题目

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.

约翰的奶牛们发现山脊上的草特别美味。为了维持草的生长,约翰打算安装若干喷灌器。
为简化问题,山脊可以看成一维的数轴,长为 L (1 <= L <= 1,000,000) ,而且L一定是一个偶数。每个喷灌器可以双向喷灌,并有确定的射程,该射程是一个整数,且不短于A,不长于B , A、B(1 <= A <= B <= 1000)都是给出的正整数。它所在位置的两边射程内,都属它的灌溉区域。
现要求山脊的每一个区域都被灌溉到,而且喷灌器的灌溉区域不允许重叠。
约翰有 N (1 <= N <= 1000)只奶牛,每一只都有特别喜爱的草区,第i只奶牛的草区是[Si,Ei],不同奶牛的草区可以重叠。现要求,每只奶牛的草区仅被一个喷灌器灌溉。

题解

设f[i]表示从0到i的位置都被覆盖所需的最小喷灌器数目
f[i]=min(f[i-k])+1———a<=k<=b
然后这样是O(L*B),1000000000会爆
于是就用单调队列优化,维护一个f[i]递增的队列,队列头是一个合法的(i-a<=j<=i-b)最小的f[j],用于更新答案

代码

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int n,l,a,b,ll,t,m;
int f[1000006],pl[1000006],val[1000006];

void insert(int i){
    while (f[i]<val[t]&&ll<=t) t--;
    pl[++t]=i;val[t]=f[i];
}

int main(){
    scanf("%d%d",&n,&l);
    scanf("%d%d",&a,&b);
    memset(f,0x3f,sizeof(f));
    m=f[0];f[0]=0;
    a*=2,b*=2;
    for (int i=1;i<=n;i++){
        int ll,rr;
        scanf("%d%d",&ll,&rr);
        for (int j=ll+1;j<rr;j++)
            f[j]=m+1;
    }
    for (int i=a;i<=b;i+=2)
        if (f[i]<=m) f[i]=1; 
    for (int i=0;i<=b-a;i+=2)
    if (f[i]<=m) insert(i); 
    for (int i=b;i<=l;i+=2)
        {
            while (pl[ll]<i-b) ll++;
            if (i!=b) insert(i-a);      
            if (f[i]<=m) f[i]=val[ll]+1;
        }
    if (f[l]>=m) printf("-1");  else
    printf("%d",f[l]);
}

猜你喜欢

转载自blog.csdn.net/yjy_aii/article/details/81771925