bzoj1986 [USACO2004 Dec] Dividing the Path 划区灌溉

http://www.elijahqi.win/archives/1092
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.

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

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

这个方程满足f[i]=minf[k]+1;

所以我们应该用单调队列处理出这个k来

求一下 我可以划分多少灌溉的区域
如果不加奶牛的限制 就可以数学方法算一算的
但是奶牛喜爱区域的开区间内只允许有一个灌溉的,那么我们认为这个区域为不可划分的就好了啊
设f[i]表示灌溉前i个区域需要最少的灌溉器的数目
按照题意的话 因为牛喜爱的牧场的开区间内是不能有多个灌溉区的 所以啊 我们就先给打上标记方便以后处理的 还有就是需要注意一下题目中给的都是到一号的距离 所以存在0的情况

每个f[i]不能刚算出来就弹队尾+进队尾,因为此时下一个位置为 i+2 ,可能会把能够转移到i+2的合法状态弹出去,而f[i]是不能转移到f[i+2]的!(因为有a的限制)所以会造成f[i+2]计算错误(当然f[l]就也有可能出错了。事实上由于我们维护的队列是一个合法状态区间,所以目前不合法的状态不应该进队,而是应该在每次更新f[i]之前让 f[i-2*a] 进队,这样可以保证队列中所有节点都为合法状态。

#include<cstdio>
#define N 1100000
#define inf 0x7f7f7f7f
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int q[N],f[N],a,b,n,l;
bool flag[N];
int main(){
    freopen("bzoj1986.in","r",stdin);
    n=read();l=read();
    a=read();b=read();
    for (int i=1;i<=n;++i) {
        int x=read(),y=read();
        for (int j=x+1;j<y;++j) flag[j]=true;
    }
    for (int i=1;i<a<<1;++i) f[i]=inf;int head=1,tail=0;
    for (int i=a<<1;i<=l;i+=2){
        while (head<=tail&&q[head]<i-b*2)++head;     
        while (head<=tail&&f[i-2*a]<f[q[tail]]) tail--;
        q[++tail]=i-2*a;
        if (flag[i]||f[q[head]]==inf) f[i]=inf;else f[i]=f[q[head]]+1;
    }
    if(f[l]==inf) printf("-1");else printf("%d",f[l]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值