• BZOJ 1986: [USACO2004 Dec] Dividing the Path 划区灌溉


    1986: [USACO2004 Dec] Dividing the Path 划区灌溉

    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

    思路:

      还是比较明显的一道题,首先区间内除了两端点都不能切开,输入时候维护一下即可,然后单调队列优化dp f[i] = min{f[k]} +1 ;

    注意A , B 都要×2;

    代码如下

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <cmath>
    using namespace std;
    const int N = 1100000;
    const int inf = 0x3f3f3f3f;
    int A, B, t, n;
    int c[N], a[N];
    int q[N], l, r;
    int f[N];
    int main() {
    	scanf("%d%d%d%d", &t, &n, &A, &B);
    	A*=2, B*=2;
    	for(int i=1;i<=t;i++) {
    		int x, y;
    		scanf("%d%d", &x, &y);
    		for(int i=x+1;i<=y-1;i++) a[i]=1;
    		
    	}
    	for(int i=1;i<A;i++) f[i] = inf;
    	for(int i=A;i<=n;i+=2) {
    		while(l<r&&q[l]<i-B)l++;
    		while(l<r&&f[i-A]<=f[q[r-1]])r--;
    		q[r++] = i-A;
    		if(a[i]||f[q[l]]==inf)f[i]=inf;
    		else f[i]=f[q[l]]+1;
    	}
    	printf("%d
    ",f[n]>1000000?-1:f[n]);
    }
    
  • 相关阅读:
    MockitoAnnotationTest 单元测试
    新建HttpServletRequest对象
    @Valid
    mysql大数据量使用limit分页,随着页码的增大,查询效率越低下
    前k小子段和、前k小子序列与前k小树上路径问题
    MySQL 执行Kill命令后,执行命令为啥没马上停止?
    MySQL 误删数据应该怎么处理?
    # vue 两个数组的差值
    # Vue 将字符串保存成 TXT 文件保存到电脑
    # vue openlayer 添加风场效果
  • 原文地址:https://www.cnblogs.com/Tobichi/p/9184735.html
Copyright © 2020-2023  润新知