poj 3017 Cut the Sequence dp+单调队列优化
来源:互联网 发布:在淘宝上怎么买开山刀 编辑:程序博客网 时间:2024/06/05 20:48
题意:
给大小为n的数组a[n]以及数m,求将a[n]划分为任意个部分后每部分最大值的最小和。比如 n=8,m=17 a[n]=2,2,2,8,1,8,2,1。答案是12,对应的划分为2.2.2。8,1,8。2,1。
思路:
dp,方程为 dp[i] = min( dp[j] + max(a[j+1,j+2,...i]) ) (j<i, sum(a[j+1,j+2,...i])<=m),这样的复杂度是n^2的,求max(j+1,j+2,...i)的步骤可以用单调队列优化。
代码:
//poj 3017//sepNINE#include <iostream>using namespace std;const int maxN = 100024;__int64 m,a[maxN],dp[maxN];int n,q[maxN];__int64 solve(){int i,j,k,front,rear;__int64 sum;for(i=1; i<=n; ++i)if(a[i]>m)return -1;front=0;rear=-1;sum=dp[0]=0;for(i=k=1; i<=n; ++i){sum+=a[i];for(; sum>m ; ++k)sum-=a[k];for(; front<=rear&&q[front]<k; ++front);for(; front<=rear&&a[q[rear]]<=a[i]; --rear);q[++rear]=i;dp[i]=dp[k-1]+a[q[front]];for(j=front; j<rear; ++j)dp[i]=min(dp[i], dp[q[j]]+a[q[j+1]]);}return dp[n];}int main(){int i;scanf("%d%I64d", &n, &m);for( i=1; i<=n; ++i )scanf("%I64d", &a[i]);printf("%I64d", solve());return 0;}
0 0
- POJ 3017 Cut the Sequence (单调队列优化DP)
- poj 3017 Cut the Sequence dp+单调队列优化
- poj 3017 Cut the Sequence(dp单调队列优化)
- POJ Cut the Sequence 单调队列优化DP入门题
- Poj 3017 Cut the Sequence (DP,单调队列优化,数据结构优化)
- 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(dp+单调队列)
- POJ-3017 Cut the Sequence(DP单调队列优化 + 平衡树)
- POJ 3017 Cut the Sequence 【DP+单调队列优化+平衡树】
- POJ 3017 Cut the Sequence 单调队列
- poj3017 Cut the Sequence 单调队列优化dp 好题!
- POJ--3017--Cut the Sequence--DP优化
- POJ 3017 Cut the Sequence 单调队列+平衡树
- POJ 3017|Cut the Sequence|动态规划|单调队列
- Cut the Sequence,Sequence Partitioning,POJ3017,POJ3245,用单调队列优化的DP
- POJ_3017 Cut the Sequence 单调队列+dp+BST
- PIL Image模块 show函数不能正常显示图片
- [Leetcode]Unique Binary Search Trees II
- IT编程宝园资料分享
- Android常用Adapter用例
- ipad air 2 使用itunes恢复更新出现3014解决方法
- poj 3017 Cut the Sequence dp+单调队列优化
- Linux给用户分配无需登陆权限
- 拖拽演示,原生封装+JQ (详解)
- 滚动条jQuery custom content scroller
- ROS环境变量的设置
- C#编码规范和命名规则
- Ubuntu12.04中JVM的安装及系统环境变量的设置
- 20140501-逐层学习 有效的解释
- 链地址处理法构造简单哈希表