POJ 3017 Cut the Sequence 单调队列+平衡树
来源:互联网 发布:淘宝全景主图制作教程 编辑:程序博客网 时间:2024/06/06 01:51
题意:给出一个序列,求将序列划分成若干段,且每段和不超过m的情况下,每段的最大值的和最小为多少。
思路:比较朴素的想法还是比较好想的,即:dp[i] = min{ dp[j]+max{ num[k] } };其中lef[i] <= j < i,(lef[i]到 i 的和最接近m,且小于m),j <= k <= i;这是n^2的复杂度。明显要超时,
所以需要优化,先用一个最大值单调队列来维护lef[i]到 i 的最大值,则dp[i] = min(dp[j] + q[head]);若num[i]为最大值,则dp[i] = dp[lef[i]-1]+num[i];若num[i]不是最大值,则需要从队列中去寻找最优解,即dp[i] = min{dp[q[j]]+num[q[j+1]]},q为单调队列,head <= j < tail;相当于将当前最大值划分到前面的集合中,或者将最大和次最大都划分到前面的集合中,以此类推。 这样也是n^2的复杂度,所以这个最小值需要用平衡树来维护,复杂度降到nlogn。
#include <cstdio>#include <cstring>#include <string>#include <iostream>#include <map>#include <set>#include <vector>#include <cmath>#include <stack>#include <queue>#include <cstdlib>#include <algorithm>using namespace std;typedef __int64 int64;typedef long long ll;#define M 100005#define N 1000005#define max_inf 0x7f7f7f7f#define min_inf 0x80808080const int mod = 1e6;int n , q[M] , num[M];ll m , dp[M];multiset<ll> sq;//平衡树int main(){int i , head , tail , cnt , fg;ll sum;while (~scanf("%d%lld",&n,&m)){sum = 0;head = tail = 0;cnt = 1;fg = 0;dp[0] = 0;dp[n] = -1;sq.clear();for (i = 1 ; i <= n ; i++){scanf("%d",num+i);if (num[i] > m)fg = 1;if (fg)continue;sum += num[i];//找寻lef[i]while (sum > m)sum -= num[cnt++];//维护单调队列while (tail > head && num[q[tail-1]] <= num[i]){tail--;if (tail > head)sq.erase(dp[q[tail-1]]+num[q[tail]]);}q[tail++] = i;//将不在lef[i]到i的值删除while (tail > head && q[head] < cnt){head++;if (tail > head)sq.erase(dp[q[head-1]]+num[q[head]]);}dp[i] = dp[cnt-1]+num[q[head]];if (tail-1 > head)sq.insert(dp[q[tail-2]]+num[q[tail-1]]);if (!sq.empty())dp[i] = min(dp[i] , *sq.begin());}printf("%lld\n",dp[n]);}return 0;}
- 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 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 不用什么堆和集合直接单调队列解决
- Poj 3017 Cut the Sequence (DP,单调队列优化,数据结构优化)
- [POJ3017] Cut the Sequence && 单调队列
- poj 3017 Cut the Sequence
- POJ 3017 Cut the Sequence
- MOS管限流
- Oracle merge into 命令
- 记录代码——快速排序
- salesforce json
- android(java) socket判断网络连接状态
- POJ 3017 Cut the Sequence 单调队列+平衡树
- zoj2972 Hurdles of 110m
- internet explorer无法打开站点问题
- Keil MDK 中利用串口及c标准库函数printf为cortex-m3做调试输出(lpc1788)
- JAVA_WEB Oracle 10g学习:PL/SQL
- Java游戏开发中应始终坚持的10项基本原则
- 数据库expecting ''', found '<EOF>'异常——原载于我的百度空间
- 50个jQuery Mobile开发技巧
- Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入