poj 3017 Cut the Sequence dp
2013-05-03 19:38
218 查看
dp[k]是非递增的,然后对于同样的转移代价,我们只需要找到i最小的dp[i]及可。
所以可以用单调队列维护一个递减的序列,存储转移代价,即一段中的最大值。对于每个转移代价下的dp[i]+a[j],把它压入set中。每次找到最小的值来更新dp[k]。
维护队列,set,有比较麻烦的边界条件,要好好处理。
所以可以用单调队列维护一个递减的序列,存储转移代价,即一段中的最大值。对于每个转移代价下的dp[i]+a[j],把它压入set中。每次找到最小的值来更新dp[k]。
维护队列,set,有比较麻烦的边界条件,要好好处理。
#include <iostream> #include <cstdio> #include <cstring> #include <set> using namespace std; const int maxn=1e5+9; typedef long long ll; multiset <ll> d; ll n,m; int a[maxn]; struct { int id,data; }que[maxn]; long long dp[maxn]; int main() { d.insert((ll)1<<50); scanf("%lld %lld",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); int st=1,ed=0,low=1; long long sum=0; for(int i=1;i<=n;i++) { if(a[i]>m) { printf("-1\n"); return 0; } sum+=a[i]; while(sum>m) sum-=a[low++]; while(ed>=st&&a[i]>=que[ed].data) { if(ed>st) d.erase(d.find(dp[que[ed-1].id]+a[que[ed].id])); ed--; } if(ed>=st) d.insert(dp[que[ed].id]+a[i]); que[++ed].data=a[i]; que[ed].id=i; while(que[st].id<low&&ed>=st) { if(st<ed) d.erase(d.find(dp[que[st].id]+a[que[st+1].id])); st++; } long long tmp=min(*d.begin(),dp[low-1]+que[st].data); dp[i]=tmp; } printf("%lld",dp ); return 0; }
相关文章推荐
- POJ-3017 Cut the Sequence DP+单调队列+堆
- poj 3017 Cut the Sequence(dp单调队列优化)
- Poj 3017 Cut the Sequence (DP,单调队列优化,数据结构优化)
- POJ 3017 Cut the Sequence(dp+单调队列)
- poj 3017 Cut the Sequence dp
- POJ - 3017 Cut the Sequence(单调队列+dp)
- poj 3017 Cut the Sequence(DP+单调队列+set)
- poj 3017 Cut the Sequence(DP+单调队列)
- POJ--3017--Cut the Sequence--DP优化
- 【单调性dp】poj 3017 Cut the Sequence
- POJ_3017 Cut the Sequence 单调队列+dp+BST
- POJ - 3017 Cut the Sequence : 单调队列优化dp
- poj 3017 Cut the Sequence dp+单调队列优化
- POJ 3017 Cut the Sequence 【DP+单调队列优化+平衡树】
- POJ-3017 Cut the Sequence(DP单调队列优化 + 平衡树)
- POJ 3017 Cut the Sequence (单调队列优化DP)
- POJ 3017 Cut the Sequence 单调队列
- POJ 3017 Cut the Sequence
- poj 3017 Cut the Sequence(单调队列优化 )
- POJ 3017|Cut the Sequence|动态规划|单调队列