Dividing the Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2858   Accepted: 1064

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[i]=min{dp[j]}+1  i-2*B=<j<=i-2*A
// 单调队列优化 求区间 dp[i-2*B] ~ dp[i-2*A] 最小值
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MOD 1000000007
#define maxn 1000010
int dp[maxn];
bool ok[maxn];
struct node{
int l,r; // 左右 下标 数值
node(){}
node(int a,int b){l=a;r=b;}
}in[],q[maxn];
int flag;
int N,L;
int A,B;
void solve(){
int i,j;
int head=,tail=,tp;
for(i=;i<=N;i++)
for(j=in[i].l+;j<in[i].r;j++)
ok[j]=;
for(i=*A;i<=L;i+=){
tp=i-*A;
if(ok[tp]){
while(head<tail&&dp[tp]<=q[tail-].r)
tail--;
if(ok[tp])
q[tail++]=node(tp,dp[tp]);
}
tp=i-*B;
while(head<tail&&q[head].l<tp)
head++; if(ok[i]&&head<tail) dp[i]=q[head].r+;//因为这里 所以最后结果可能 >=MOD wa的我好难过
}
}
int main()
{ dp[]=;
while(scanf("%d %d",&N,&L)!=EOF){
scanf("%d %d",&A,&B);
int i;
flag=true;
for(i=;i<=N;i++){
scanf("%d %d",&in[i].l,&in[i].r);
if(in[i].r-in[i].l>*B)
flag=false;
}
ok[]=;
if(flag){
for(i=;i<=L;i++)
ok[i]=,dp[i]=MOD;
solve();
}
if(!flag||dp[L]>=MOD) // 就是因为这里没有写 成 >= 而写成 == wa的我好难过 不过还好 OMs
printf("-1\n");
else
printf("%d\n",dp[L]);
} return ;
}

poj 2373 Dividing the Path的更多相关文章

  1. POJ 2373 Dividing the Path(DP + 单调队列)

    POJ 2373 Dividing the Path 描述 农夫约翰的牛发现,在他的田里沿着山脊生长的三叶草是特别好的.为了给三叶草浇水,农夫约翰在山脊上安装了喷水器. 为了使安装更容易,每个喷头必须 ...

  2. POJ 2373 Dividing the Path (单调队列优化DP)题解

    思路: 设dp[i]为覆盖i所用的最小数量,那么dp[i] = min(dp[k] + 1),其中i - 2b <= k <= i -2a,所以可以手动开一个单调递增的队列,队首元素就是k ...

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

    题目 传送门:QWQ 分析 听说是水题,但还是没想出来. $ dp[i] $为$ [1,i] $的需要的喷头数量. 那么$ dp[i]=min(dp[j])+1 $其中$ j<i $ 这是个$ ...

  4. [POJ 2373][BZOJ 1986] Dividing the Path

    Link: POJ 2373 传送门 Solution: 一开始想错方向的一道简单$dp$,不应该啊…… 我一开始的想法是以$cows' ranges$的节点为状态来$dp$ 但明显一个灌溉的区间的两 ...

  5. poj 3764 The xor-longest Path(字典树)

    题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...

  6. poj2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5060   Accepted: 1782 ...

  7. Dividing the Path POJ - 2373(单调队列优化dp)

    给出一个n长度的区间,然后有一些小区间只能被喷水一次,其他区间可以喷水多次,然后问你要把这个区间覆盖起来最小需要多少喷头,喷头的半径是[a, b]. 对于每个只能覆盖一次的区间,我们可以把他中间的部分 ...

  8. Dividing the Path POJ - 2373 dp

    题意:你有无数个长度可变的区间d  满足 2a<=d<=2b且为偶数. 现在要你用这些区间填满一条长为L(L<1e6且保证是偶数)的长线段. 满足以下要求: 1.可变区间之间不能有 ...

  9. [USACO2004][poj2373]Dividing the Path(DP+单调队列)

    http://poj.org/problem?id=2373 题意:一条直线分割成N(<=25000)块田,有一群奶牛会在其固定区域吃草,每1把雨伞可以遮住向左右延伸各A到B的区域,一只奶牛吃草 ...

随机推荐

  1. c# 与 Unity3d 中的序列化

    圣典中对于Unity3D的序列化介绍很容易和C#的序列化介绍搞混,做个笔记,方便以后查找. 很多资料算是拾人牙慧. 一.Serializable 序列化 Inherits from Attribute ...

  2. 浏览器后退按钮导致jquery动态添加的select option值丢失的解决方法

    监控浏览器返回功能 判断浏览器返回功能 禁用浏览器的后退按钮 JS禁止浏览器后退键 http://volunteer521.iteye.com/blog/830522/ 浏览器返回功能 判断上一页面来 ...

  3. 【好玩的应用】QQ连连看辅助工具

    自己学了这么久的C语言,但没有写出过什么可以用的东西来,总觉得心里不爽.这几天实在是不想干正事,在网上瞎逛逛,结果发现有人写了连连看的外挂.顿时觉得这很有意思啊.于是把代码下载下来,捣鼓了捣鼓.发现还 ...

  4. JsRender系列-11

    <!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...

  5. hdu 4704 Sum

    思路:对于给定的n,s(i)即将n分解为i个数的组合数,也就是在n-1个位置插入i-1个板即C(n-1,i-1); ∑S=2^(n-1); phi(1000000007)=1000000006; 对于 ...

  6. Play Framework 发现并没有热启动的特殊情况

    解决办法: 删掉 target目录下的两个文件夹: src_mananger 和 twirl -----或者删掉整个target文件夹. 因为play framework 运行的是 在target 文 ...

  7. 毕向东JAVA视频视频讲解(第八课)

    继承的好处: 1,提高了代码的复用性. 2,让类与类之间产生了关系,给第三个特征多态提供了前提. java中支持单继承.不直接支持多继承,但对C++中的多继承机制进行改良. 单继承:一个子类只能有一个 ...

  8. java:IO-读写大文件

    import java.io.*; class Test { public static void main(String args[]){ FileInputStream fin =null; Fi ...

  9. C++:常类型Const

    常类型:使用类型修饰符const说明的类型,常类型的变量或对象成员的值在程序运行期间是不可改变的. 3.10.1 常引用 如果在说明引用时用const修饰,则被说明的引用为常引用.如果用常引用做形参, ...

  10. 机器学习 —— log-linear 模型

    昨天刚刚解决了 logistic regression 之后今天又来了个有趣的家伙. logistic regression 很强大,但是也有它的弱点.它最大的弱点就是只能告诉你是或者不是,而无法告诉 ...