zoukankan      html  css  js  c++  java
  • POJ2373 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.

    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.

    Source

     
     
    正解:DP+单调队列优化
    解题报告:
      单调队列裸题,在POJSC的时候也考了这道原题,当时没做出来。。。然而今天调了两个多小时。。。
      考虑直接DP肯定是f[i]表示到i之前最小的线段数,那么只需要枚举题目给的[a,b]区间即可。显然会TLE。
      在枚举的时候会发现有很多显然不优的答案根本没有必要枚举,直接单调队列维护一个[a*2,b*2]的滑动窗口,直接做就可以了。
      我又犯了一些愚蠢的错误,比如说显然长度必须为偶数我没考虑到,而且应该从b*2开始,但是之前的(b*2-a*2)必须先插进单调队列。改了就可以AC了。
     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #include <set>
    13 #ifdef WIN32   
    14 #define OT "%I64d"
    15 #else
    16 #define OT "%lld"
    17 #endif
    18 using namespace std;
    19 typedef long long LL;
    20 const int MAXL = 1000011;
    21 const int inf = (1<<29);
    22 int n,a,b,L;
    23 int f[MAXL];
    24 int head,tail;
    25 struct que{
    26     int id,val;
    27 }dui[MAXL*4];
    28 
    29 inline int getint()
    30 {
    31        int w=0,q=0;
    32        char c=getchar();
    33        while((c<'0' || c>'9') && c!='-') c=getchar();
    34        if (c=='-')  q=1, c=getchar();
    35        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
    36        return q ? -w : w;
    37 }
    38 /*
    39 inline void DP(){  
    40     f[0]=0;
    41     for(int i=a*2;i<=L;i++)  {
    42     if(f[i]==inf+1) continue;
    43     for(int j=a;j<=b;j++) {
    44         if(i-2*j<0) break;
    45         if(f[i-2*j]!=inf+1) { f[i]=min(f[i],f[i-2*j]+1); }
    46     }
    47     }
    48     printf("%d",f[L]);
    49     }*/
    50 inline void DP(){
    51     f[0]=0;head=1,tail=0;   dui[0].val=0; 
    52     
    53     for(int i=0;i<=b*2-a*2;i+=2) {
    54     while(head<=tail && f[i]<=dui[tail].val) tail--;
    55     dui[++tail].val=f[i]; dui[tail].id=i;
    56     while(head<=tail && i-dui[head].id>2*b) head++;    
    57     }
    58     for(int i=b*2;i<=L;i+=2) {//奇数不可能        
    59     while(head<=tail && i-dui[head].id>2*b) head++;    
    60     if(i>2*b && f[i-2*a]<inf) {
    61         while(head<=tail && f[i-2*a]<=dui[tail].val) tail--;
    62         dui[++tail].val=f[i-2*a]; dui[tail].id=i-2*a;    
    63     }
    64     if(f[i]==inf+1) continue;
    65     if(head<=tail) f[i]=dui[head].val+1;
    66     }
    67     if(f[L]>=inf) printf("-1");
    68     else printf("%d",f[L]);
    69 }
    70 
    71 inline void work(){
    72     n=getint(); L=getint(); a=getint(); b=getint();
    73     int x,y; for(int i=0;i<=L+1;i++) f[i]=inf;    
    74     for(int i=1;i<=n;i++) {//区间的内部不可能作为端点,打上特殊标记
    75     x=getint(); y=getint();
    76     for(int j=x+1;j<y;j++) f[j]=inf+1;
    77     }
    78     for(int i=a;i<=b;i++) if(f[i*2]<=inf) f[i*2]=1;   //0要算在内
    79     if(L&1) { printf("-1"); return ; }
    80     DP();
    81 }
    82 
    83 int main()
    84 {
    85   work();
    86   return 0;
    87 }
  • 相关阅读:
    OpenLayer学习之图文标注
    HTML5制作时钟(canvas)
    ADO,NET中简单三层SQLHelper封装介绍
    OpenLayer学习之加载天地图
    ArcGIS Server manger管理页面无法打开问题解决
    Java EE (14) -- SSH配置
    R语言实战读书笔记(一)R语言介绍
    Machine Learning for hackers读书笔记_一句很重要的话
    Machine Learning for hackers读书笔记(十二)模型比较
    Machine Learning for hackers读书笔记(十)KNN:推荐系统
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5734268.html
Copyright © 2011-2022 走看看