POJ 3017|Cut the Sequence|动态规划|单调队列
来源:互联网 发布:淘宝领券口令怎么用 编辑:程序博客网 时间:2024/05/23 21:47
给定一个数列
其中
时间复杂度是
开一个单调队列,使得队首到当前位置的和不超过m,那么单调队列里随便哪个元素都是满足要求的:
对于单调队列的最小值,set维护即可。
#include <cstdio>#include <set>using namespace std;typedef long long ll;const int N = 100005;int q[N], dp[N], a[N];multiset<int> s;ll calc(int n, ll m) { int i, j = 1, k, f = 0, r = 0; ll sum = 0; for (i = 1; i <= n; ++i) { sum += a[i]; while (sum > m) sum -= a[j++]; if (j > i) return -1; while (f < r && a[q[r - 1]] <= a[i]) { if (r - f >= 2) s.erase(dp[q[r - 2]] + a[q[r - 1]]); --r; } q[r++] = i; if (r - f >= 2) s.insert(dp[q[r - 2]] + a[q[r - 1]]); while (f < r && q[f] < j) { if (r - f >= 2) s.erase(dp[q[f]] + a[q[f + 1]]); ++f; } dp[i] = dp[j - 1] + a[q[f]]; if (r - f >= 2) dp[i] = min(dp[i], *s.begin()); } return dp[n];}int main() { int i, n; ll m; scanf("%d%lld", &n, &m); for (i = 1; i <= n; ++i) scanf("%d", &a[i]); return printf("%lld", calc(n, m)), 0;}
Cut the Sequence
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 9998 Accepted: 2999
Description
Given an integer sequence { an } of length N, you are to cut the sequence into several parts every one of which is a consecutive subsequence of the original sequence. Every part must satisfy that the sum of the integers in the part is not greater than a given integer M. You are to find a cutting that minimizes the sum of the maximum integer of each part.
Input
The first line of input contains two integer N (0 < N ≤ 100 000), M. The following line contains N integers describes the integer sequence. Every integer in the sequence is between 0 and 1 000 000 inclusively.
Output
Output one integer which is the minimum sum of the maximum integer of each part. If no such cuttings exist, output −1.
Sample Input
8 17
2 2 2 8 1 8 2 1
Sample Output
12
Hint
Use 64-bit integer type to hold M.
Source
POJ Monthly–2006.09.29, zhucheng
- 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 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+单调队列优化+平衡树】
- [POJ3017] Cut the Sequence && 单调队列
- POJ 3245|Sequence Partitioning|动态规划|单调队列
- poj 3017 Cut the Sequence
- 《第一行代码》学习笔记之通知的基本用法
- HashMap和HashTable区别
- 第四次上机实验报告—三色球问题
- java开发环境搭建
- 解决Scrapy性能问题——案例三(下载器中的“垃圾”)
- POJ 3017|Cut the Sequence|动态规划|单调队列
- 设计心理学读书笔记
- CSS中如何选择ul下li的奇数、偶数行
- 简单的权限声明总结
- HOG特征
- 关于监听切换应用,app从后台返回进入锁屏界面
- 异常
- 集成融云Android SDK实现在群聊/讨论组中@人的功能(三)
- tortoiseSVN解决冲突