poj2373 Dividing the Path 单调队列dp
2014-11-09 15:53
113 查看
题意:在长为L的草地上装喷水头,喷射是以这个喷水头为中心,喷水头的喷洒半径是可调节的,调节范围为[a,b]。要求草地的每个
点被且只被一个喷水头覆盖,并且有些连续区间必须被某一个喷水头覆盖,而不能由多个喷头分段完全覆盖,求喷水头的最小数目。
思路:设dp[i]表示正好喷到i这个位置缩安放的最少喷泉数,那么dp[i]=min(dp[k])+1 (i - 2 * B <= k <= i - 2 * A)
但是有些区间连续区间必须只能被一个喷水头覆盖,不妨设区间为[l,r],那么对于dp方程来说,区间[l+1,r-1]都不能作为中间转移
过程(即不存在dp[i],其中l+1<=i<=r-1),所以我们用st数组标记该点是否可行。之后维护一个单调队列即可,不过只需考虑偶数就
行,因为半径*2为偶数,所以不可能存在i为奇数的情况。
ps:有一个wa点,就是需要注意注意当前考虑的点是否可行,不可行的话不能更新。详见代码:
点被且只被一个喷水头覆盖,并且有些连续区间必须被某一个喷水头覆盖,而不能由多个喷头分段完全覆盖,求喷水头的最小数目。
思路:设dp[i]表示正好喷到i这个位置缩安放的最少喷泉数,那么dp[i]=min(dp[k])+1 (i - 2 * B <= k <= i - 2 * A)
但是有些区间连续区间必须只能被一个喷水头覆盖,不妨设区间为[l,r],那么对于dp方程来说,区间[l+1,r-1]都不能作为中间转移
过程(即不存在dp[i],其中l+1<=i<=r-1),所以我们用st数组标记该点是否可行。之后维护一个单调队列即可,不过只需考虑偶数就
行,因为半径*2为偶数,所以不可能存在i为奇数的情况。
ps:有一个wa点,就是需要注意注意当前考虑的点是否可行,不可行的话不能更新。详见代码:
// file name: poj2373.cpp // // author: kereo // // create time: 2014年11月09日 星期日 10时21分51秒 // //***********************************// #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<set> #include<map> #include<vector> #include<stack> #include<cmath> #include<string> #include<algorithm> using namespace std; typedef long long ll; const int sigma_size=26; const int MAXN=1000000+100; const double eps=1e-8; const int inf=0x3fffffff; const int mod=1000000000+7; #define L(x) (x<<1) #define R(x) (x<<1|1) int n,m,A,B,head,tail; int dp[MAXN],st[MAXN]; struct node{ int index,val; }que[MAXN]; int main() { while(~scanf("%d%d",&n,&m)){ scanf("%d%d",&A,&B); for(int i=0;i<=m;i++) dp[i]=inf,st[i]=1; dp[0]=0; int l,r; int flag=1; for(int i=1;i<=n;i++){ scanf("%d%d",&l,&r); if(r-l>2*B) flag=0; for(int j=l+1;j<r;j++) st[j]=0; } if(!flag){ printf("-1\n"); continue; } head=tail=0; for(int i=2*A;i<=m;i+=2){ int k=i-2*A; while(head<tail && que[tail-1].val>dp[k] && st[k]) tail--; que[tail].val=dp[k]; que[tail++].index=k; if(!st[i]) continue; while(head<tail && que[head].index<i-2*B) head++; dp[i]=que[head].val+1; } if(dp[m]>=inf){ printf("-1\n"); continue; } printf("%d\n",dp[m]); } return 0; }
相关文章推荐
- 【poj2373】Dividing the Path【单调队列优化dp】
- [USACO2004][poj2373]Dividing the Path(DP+单调队列)
- POJ 2373 (Dividing the Path)单调队列优化DP
- poj 2373 Dividing the Path(dp+单调队列优化)
- POJ 2373 Dividing the Path(队列dp)
- poj 3017 Cut the Sequence(DP+单调队列)
- POJ 3017 Cut the Sequence(dp+单调队列)
- POJ 3017 Cut the Sequence 【DP+单调队列优化+平衡树】
- 20152016-acmicpc-neerc-northern-subregional-contest J:Journey to the "The World's Start"(单调队列+DP+二分)
- GYM 100801J Journey to the “The World's Start”【二分+Dp+单调队列】
- Dividing(多重背包、单调队列优化dp)
- POJ2373 Dividing the Path
- POJ - 3017 Cut the Sequence(单调队列+dp)
- POJ Cut the Sequence 单调队列优化DP入门题
- poj2373 Dividing the Path
- POJ 3017 Cut the Sequence (单调队列优化DP)
- POJ2373 Dividing the Path——动态规划+单调队列优化
- Poj 3017 Cut the Sequence (DP,单调队列优化,数据结构优化)
- POJ - 3017 Cut the Sequence : 单调队列优化dp
- poj 3017 Cut the Sequence(DP+单调队列+set)