【单调性dp】poj 3017 Cut the Sequence
来源:互联网 发布:韩国东大门淘宝店推荐 编辑:程序博客网 时间:2024/05/22 15:11
题目:http://poj.org/problem?id=3017
参考:http://www.cnblogs.com/MashiroSky/p/5956429.html
//看了好久,智商堪忧//#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;/*题意:给出一个数列(n个数)要求将它分割成许多块,每块的数的和不超过m,要求每块中最大的数之和最小。题解:dp[i]表示处理到第i个数,并且第i个数作为分割节点,每块中最大的数之和最小的指。朴素转移方程式:dp[i] = min(dp[xi]+Max(a[j]));(xi<i)(j>xi,j<=i)(Sum(a[j])<=m)对于a[xi+1]-->a[i]的最大值位置设为k,那么对于(xi<k),dp[i]=min(dp[xi])+a[k],可以发现dp[i]是单调不降的,所以对于(xi<k),dp[i]=dp[min(xi)]+a[k];对于(xi>=k),dp[i]=dp[k]+Max(a[k+1]-->a[i])。那么用单调队列维护a[i]最大值的位置,由于k后面的范围无法用优先级比较,我们需要用multiset维护最小的a[k]+dp[xi]。*/const int N = 100010;int a[N],q[N];LL pre[N],dp[N],m;int main(){ int n; scanf("%d%lld",&n,&m); pre[0] = 0; for(int i=1; i<=n; i++){ scanf("%d",&a[i]); pre[i] = pre[i-1]+a[i]; } multiset<LL> s; int head=1,tail=0; int k = 0; for(int i=1; i<=n; i++) { if(a[i]>m){ puts("-1"); return 0; } while(pre[i]-pre[k]>m) k++; while(head<=tail && q[head]<=k){ //弹出头部元素时,头部的后一个元素(如果有)即是我们说的k if(head<tail) s.erase(a[q[head+1]]+dp[q[head]]); head++; } while(head<=tail && a[i]>=a[q[tail]]){ if(head<tail) s.erase(a[q[tail]]+dp[q[tail-1]]); tail--; } q[++tail]=i; if(head<tail && i>q[tail-1]) s.insert(a[i]+dp[q[tail-1]]); dp[i] = a[q[head]]+dp[k]; LL tmp = *s.begin(); if(head<tail && tmp<dp[i]) dp[i] = tmp; } printf("%lld\n",dp[n]); return 0;}
阅读全文
0 0
- 【单调性dp】poj 3017 Cut the Sequence
- 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+单调队列+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 3017 Cut the Sequence
- POJ 3017 Cut the Sequence
- POJ 3017 Cut the Sequence
- POJ 3017 Cut the Sequence
- hdu5064 Find Sequence 单调性dp
- POJ 3017 Cut the Sequence 单调队列
- POJ 3017 Cut the Sequence 笔记
- win10状态下安装siteserver5.0
- Spring AOP 面向切面编程
- B
- vue-router 动态添加 路由
- Angular4 第五章 数据绑定
- 【单调性dp】poj 3017 Cut the Sequence
- 使用递归的方式显示树形结构
- 软件工程作业
- 2017 年初、阿里、腾讯、百度、华为、京东、搜狗和滴滴面试题汇集
- 数组,指针与函数之间的关系
- 用JAVA写一个简单的21点游戏(不设置庄主的情况下)
- 通过ajaxFileUpload控件上传图片,java后台接收并保存本地
- VitualBox安装centos虚拟机
- 数据结构实验之图论三:判断可达性(BFS算法)