poj 3017 Cut the Sequence(DP+单调队列)
来源:互联网 发布:淘宝昵称是什么 编辑:程序博客网 时间:2024/06/06 03:37
题意,给定一个序列,让你把它分割成N个子串,使得每个子串的和<=m并且每个子串的最大值加起来要最小。
DP解:方程为 DP[i]=max(DP[i],DP[p-1]+ max(a[p]~~a[i]) ),P为当前分割的位置,i为当前元素下标。 Sum( a[p]~~a[i] )要满足<=m。
而p要从p一直枚举到j,因为这之间任意一个位置作为分割点都有可能产生最优解。
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;#define N 100000long long a[N+5],dp[N+5];long long q[N+5];inline long long ReadInt(){ char ch = getchar(); long long data = 0; while (ch < '0' || ch > '9') { ch = getchar(); } do { data = data*10 + ch-'0'; ch = getchar(); }while (ch >= '0' && ch <= '9'); return data;}int main(){ long long n,m; while(~scanf("%lld%lld",&n,&m)) { int flag=1; for(int i=0;i<n;i++) { a[i]=ReadInt(); if(a[i]>m) { flag=0; } } if(!flag){ printf("-1\n");continue;} int front=0,rear=0,p=0; long long sum=a[0]; dp[0]=a[0]; dp[1]=a[1]; q[rear++]=0; for(int i=1;i<n;i++){sum+=a[i];while (sum>m&&p<i){sum-=a[p];p++;}while (front<rear&&a[q[rear-1]]<= a[i]){rear--;}q[rear++] = i;while (q[front]<p && front<rear){front++;}dp[i] = dp[p-1] + a[q[front]];for (int j=front;j<rear-1; j++){dp[i]=min(dp[i],dp[q[j]] + a[q[j + 1]]);}} printf("%lld\n",dp[n-1]); } return 0;}
- 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(dp+单调队列)
- POJ 3017 Cut the Sequence 单调队列
- POJ 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 单调队列+dp+BST
- poj3017 Cut the Sequence 单调队列优化dp 好题!
- poj 3017 Cut the Sequence 不用什么堆和集合直接单调队列解决
- poj 3017 Cut the Sequence dp
- VC6.0开发OCX按钮控件
- 笔记
- 物化视图
- UVA 548 tree
- sqlconnection,sqlcommand,sqldataadapter,sqldatareader,dataset
- poj 3017 Cut the Sequence(DP+单调队列)
- MyBatis的动态SQL详解
- java自定义注解
- 程序员如何保持优秀
- C++语法基础--顺序容器(五)--容器适配器-- queue,priority_queue,stack
- DWZ (JUI) 教程 可以伸缩的查询面板 (searchBar)
- UVA 539 The Settlers of Catan
- DWZ 富客户端框架设计思路与学习建议
- 正则表达式初步