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

USACO 2004 December Gold

正解: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 }
时间: 2024-08-01 22:31:32

POJ2373 Dividing the Path的相关文章

poj2373 Dividing the Path (单调队列+dp)

题意:给一个长度为L的线段,把它分成一些份,其中每份的长度∈[2A,2B]且为偶数,而且不能在某一些区间内部切开,求最小要分成几份 设f[i]为在i处切一刀,前面的满足要求的最小份数,则f[L]为答案 f[i]=min(f[j])+1,2A<=i-j<=2B,i,j可以切维护一个单调队列,每次取出来f[i-(2B-2A)..i]的最小值,给到f[i+2A]即可 1 #include<cstdio> 2 #include<cstring> 3 #include<al

POJ 2373 Dividing the Path(线段树+dp)

Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3798   Accepted: 1363 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 clo

poj 2373 Dividing the Path

http://poj.org/problem?id=2373 题意: 在长为L的草地上装喷水头,喷水头的喷洒半径为[a,b] 要求草地的每个整点被且只被一个喷水头覆盖 有N个特殊区间,要求只能被某一个喷水头完整地覆盖,而不能由多个喷水头分段覆盖 求喷水头的最小数目 喷水头只能建在整数点上 f[i] 表示区间[0,i]完全被覆盖的最少喷水头数目 f[i]=min(f[j]+1)   j∈[i-2*a,i-2*b] 若i处于某个特殊区间内部,则f[i]=inf 单调队列优化 注意因为喷水头只能建在整

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

L<=1000000的土地上用长度在2*A~2*B的线段覆盖所有点,且给定n<=1000个区间,每个区间上只允许有一条线段,求最少多少线段,无解-1. f[i]表示填前i个土地最少线段,f(i)=f(j)+1,2*A<=i-j<=2*B,用个单调队列就行.注意区间是左闭右开. 至于那些坏区间,如果某个状态在覆盖了该点的最远的右端点,那是合法的,否则一旦它被覆盖就是不合法状态. 为了处理这种情况,将区间按左端点排序,走到一个点时,若该点满足上面的情况,那就说明有一些区间的左端点<

【POJ】2373 Dividing the Path(单调队列优化dp)

题目 传送门:QWQ 分析 听说是水题,但还是没想出来. $ dp[i] $为$ [1,i] $的需要的喷头数量. 那么$ dp[i]=min(dp[j])+1 $其中$ j<i $ 这是个$ O(n^2)$的东西,用单调队列优化一下就行了 复杂度$ O(L) $ 代码 #include <bits/stdc++.h> using namespace std; const int maxn=250; int A[maxn][maxn], num[maxn*maxn], id[100000

单调队列题目练习

RT.由于本人dp很弱(或者说什么都弱),于是决定分模块刷题.单调队列就找了些题目(我水平已经沦落到了普及组qwq)练,顺便把以前做过的题都堆起来.以后做到的题再开新文章. 1.多重背包 不说了,很好推.放许久之前的幼稚代码 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAXN=1000+7; 4 const int MAXV=10000+7; 5 inline int Read(){ 6 int x=0,f=0;c

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116