刷题总结——Cut the Sequence(POJ 3017 dp+单调队列+set)
题目:
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.
题解:
哇塞···这道题太bt了·····
引用sdj222555题解:%%%%%%%
令f[i] 表示前i个数按照题目要求的最小的和
则必然有f[i] = min(f[j] + max(a[j +1 , a[j + 2].....a[i]))
其中j<= i,j的位置还得满足题目中m 的限制
由于a数组都是大于0的,所以可以发现f必然是非递减的。
设a[j + 1], a[j + 2], ...a[i]中值最大的下标为k
设x为[j + 1,k]的任意一个下标,则a[x],a[x+1],....a[i]的最大值的下标显然也是k了
由f的非递减性,f[j+1] + a[k] <= f[j+2]+a[k].....<= f[k - 1] + a[k]
很显然,我们只要取f[j+1]+a[k]就可以了。
也就是说如果某一段到当前i位置的最大值都一样,取最靠前的即可。
如何维护呢,可以联想到单调队列。
维护一个递减的队列,存的是符合要求的某一段的最大值,但是可以发现,并不是队首元素就是最优,因为队列中的递减性质,队列中的所有元素都有可能导致最优解。
这时可以用到的东西就很多了,堆啊,各种树这样的。实际上,用个set可以有效的减少代码量。
那么为什么不用递增的队列呢? 如果用递增,比如队列中存的是a[1] a[2] a[3] (a[1] < a[2] < a[3]) ,现在的位置是x,那么a[1], a[2] a[3]到x位置的最大值是相等的。那么a[2]和a[3]就没有存在的意义了。然后就又变成了递减的序列。
最后不得不说比较难想的地方在于a在单调队列中是递减的··这也是关键部分····
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cctype>
#include<set>
using namespace std;
const int N=1e5+;
int num[N],que[N],le,ri,n,low,maxx=;
long long tot,m,f[N];
multiset<int>st;
int main()
{
//freopen("a.in","r",stdin);
scanf("%d%I64d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
if(num[i]>m) {cout<<"-1"<<endl;return ;}
}
low=,le=,ri=;
for(int i=;i<=n;i++)
{
tot+=num[i];
while(tot>m) tot-=num[low++];
while(le<=ri&&num[i]>=num[que[ri]])
{
if(ri>le) st.erase(f[que[ri-]]+num[que[ri]]);ri--;
}
que[++ri]=i;
if(ri>le) st.insert(f[que[ri-]]+num[que[ri]]);
while(que[le]<low)
{
if(le<ri) st.erase(f[que[le]]+num[que[le+]]);le++;
}
int temp=*st.begin();f[i]=f[low-]+num[que[le]];
if(le<ri&&temp<f[i]) f[i]=temp;
}
cout<<f[n]<<endl;
return ;
}
刷题总结——Cut the Sequence(POJ 3017 dp+单调队列+set)的更多相关文章
- POJ 3017 DP + 单调队列 + 堆
题意:给你一个长度为n的数列,你需要把这个数列分成几段,每段的和不超过m,问各段的最大值之和的最小值是多少? 思路:dp方程如下:设dp[i]为把前i个数分成合法的若干段最大值的最小值是多少.dp转移 ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- POJ - 3162 Walking Race 树形dp 单调队列
POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...
- POJ 2823【单调队列】
题意: 给出序列,找出每个连续长度为k的子序列的最大值和最小值. 思路: 裸单调队列... 单调队列这东西用的真的非常局限,大概只能用到这种情景中== 简单说一下维护: 添加元素,为了保持单调性,排除 ...
- poj 1821 Fence 单调队列优化dp
/* poj 1821 n*n*m 暴力*/ #include<iostream> #include<cstdio> #include<cstring> #incl ...
- POJ 1742 Coins ( 单调队列解法 )
id=1742" target="_blank">题目链接~~> 做题感悟:第一次做的时候用的二进制优化.可是没注意到是险过.so也没去看单调队列的解法. 解 ...
- BZOJ5090 组题 BZOJ2017年11月月赛 二分答案 单调队列
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ5090 11月月赛A题 题意概括 给出n个数. 求连续区间(长度大于等于k)最大平均值. 题解 这题 ...
- POJ 3162 Walking Race(树形dp+单调队列 or 线段树)
http://poj.org/problem?id=3162 题意:一棵n个节点的树.有一个屌丝爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...
- poj 3162 树DP+单调队列
http://acm.hust.edu.cn/vjudge/problem/11552 http://blog.csdn.net/woshi250hua/article/details/7727677 ...
随机推荐
- 将数据表中的数据添加到ComboBox控件中
实现效果: 知识运用: ComboBox控件的DataSource 属性 //获取或设置ComboBox的数据源 public Object DataResouce{get;set;} //属性值:任 ...
- 2018.4.6 java交易记录系统
题目 ###1.交易明细文件内容如下例: 客户号 姓名 所述机构号 性别 帐号 发生时间 发生额 000001|刘德华|0000|1|4155990188888888|20060720200005|3 ...
- bower使用
1,先安装nodejs(npm),Git 2,安装bower cmd执行到在项目文件夹下路径,执行npm install bower 3,执行bower init 项目根目录下将生成bower.js ...
- 数据类型-------JavaScript
之前只是简单的学过JavaScript和JQuery,虽然一般的要求都能完成,但并没有深入,这次是看了一个网站,很详细的教学,想重新认识一下JavaScript和JQuery. 本文摘要:http:/ ...
- iOS跳转到各种系统设置界面
定位服务 定位服务有很多APP都有,如果用户关闭了定位,那么,我们在APP里面可以提示用户打开定位服务.点击到设置界面设置,直接跳到定位服务设置界面.代码如下: //定位服务设置界面 NSURL *u ...
- NSRegularExpression 使用
需求: // 后台返回的某个实体 reminder = { cost = , type = , template = 可免费做某事{time}分钟,超过将按{cost}K元收费, time = } t ...
- 网络流(一)——Edmonds Karp算法
首先是一些关于网络流的术语: 源点:即图的起点. 汇点:即图的终点. 容量:有向边(u,v)允许通过的最大流量. 增广路:一条合法的从源点流向汇点的路径. 网络流问题是在图上进行解决的,我们通常可以将 ...
- 【jenkins】jenkins执行nohup java报错
nohup:failed to run command 'java':No such file or directory 这是因为jenkins只认绝对路径.在shell里面有涉及到文件的都应该写成绝 ...
- destoon 列表页面增加手动选择排序方式
在mobile/include/mall.inc.php 行60 $order = $MOD['order']; 之前增加 排序方式判断 如果有order参数则$order接受参数,没有就用默认 ...
- apply 与 lambda
Python中的lambda和apply用法 https://blog.csdn.net/anshuai_aw1/article/details/82347016