POJ 3017 Cut the Sequence
来源:互联网 发布:通达信期货 mac 编辑:程序博客网 时间:2024/05/16 16:19
写的第一个单调队列优化DP,虽然优化的地方还是有点不懂,m是那个限制的最大值
写个粗暴的状态转移方程吧dp[i]=min(dp[i-m]+max(i-m到i区间内));
i表示第几个数字,dp[i-m]中的i-m,提前预处理一下,求出是第几个位置;
然后就是优化了。。。优化部分就不说了,说不好。
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;__int64 a[100005],id[100005];__int64 sum[100005],num[100005];__int64 dp[100005];int main(){ __int64 i,j,n,m; while(scanf("%I64d %I64d",&n,&m)!=EOF) { for(i=0;i<=n;i++) sum[i]=0; dp[0]=0; memset(num,0,sizeof(num)); memset(id,0,sizeof(id)); bool flag=1; __int64 st=0; for(i=1;i<=n;i++) { scanf("%I64d",&a[i]); if(a[i]>m) flag=0; if(flag==1) { sum[i]=a[i]+sum[i-1]; while(sum[i]-sum[st]>m) st++; id[i]=st; } } if(flag==0) {printf("-1\n"); continue;} __int64 first=0,last=1; for(i=1;i<=n;i++) { while(first<last&&a[i]>a[num[last-1]]) last--; num[last++]=i; while(num[first]<id[i]) first++; dp[i]=dp[id[i]]+a[num[first]]; for(j=first;j<last;j++) dp[i]=min(dp[i],dp[num[j]]+a[num[j+1]]); } printf("%I64d\n",dp[n]); } return 0;}
0 0
- poj 3017 Cut the Sequence
- POJ 3017 Cut the Sequence
- POJ 3017 Cut the Sequence
- POJ 3017 Cut the Sequence
- POJ 3017 Cut the Sequence
- 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 dp 单调队列+set
- poj 3017 Cut the Sequence(DP+单调队列+set)
- poj 3017 Cut the Sequence(DP+单调队列)
- POJ 3017 Cut the Sequence 单调队列+平衡树
- 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|动态规划|单调队列
- 64位 windows下注册nginx、tomcat、mysql为服务
- wifidog 源码初分析(2)
- 培训总结
- cocos2dx 3.0 windows8下开发环境搭建搭建 不需要cygwin
- 稳定婚姻问题
- POJ 3017 Cut the Sequence
- motion中的motion.conf的一种配置
- Linux查看端口使用情况
- 一些概念
- 黑马程序员-Java循环结构
- Sublime Text 2 入门及技巧
- Android之NDK
- 【不借助MyEclipse,手动写自己的第一个web项目】
- 第01讲-枚举与剪枝_观察算式