题目链接:http://poj.org/problem?id=3017

  这题的DP方程是容易想到的,f[i]=Min{ f[j]+Max(num[j+1],num[j+2],......,num[i]) | 满足m的下界<j<=i },复杂度O(n^2),妥妥的TLE。其实很多都决策都是没有必要的,只要保存在满足m的区间内,num值单调递减的的那些决策。如果遍历的话,一个下降的序列会退化到O(n^2),于是用堆来优化。。。堆优化这里,纠结了很久T_T,,,网上很多代码都是直接用set来处理,但是set在erase元素的都是会把相同的元素都除掉,应该是只erase一个元素,因为相同的元素中其它的可能会存在队列中。。。难道是数据弱了?。。。

 //STATUS:C++_AC_1172MS_1352KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int num[N],q[N];
int n;
LL m,f[N];
multiset<int> sbt; int main()
{
// freopen("in.txt","r",stdin);
int i,j,l,r,p,ok;
LL sum;
while(~scanf("%d%I64d",&n,&m))
{
l=sum=;r=-;
sbt.clear();
ok=;
for(i=p=;i<=n;i++){
scanf("%d",&num[i]);
sum+=num[i];
while(sum>m)sum-=num[p++];
if(p>i){ok=;break;}
while(l<=r && num[i]>=num[q[r]]){
if(l<r)sbt.erase(f[q[r-]]+num[q[r]]);
r--;
}
q[++r]=i;
if(l<r)sbt.insert(f[q[r-]]+num[q[r]]);
while(q[l]<p){
if(l<r)sbt.erase(f[q[l]]+num[q[l+]]);
l++;
}
f[i]=f[p-]+num[q[l]];
if(l<r)f[i]=Min(f[i],(LL)*sbt.begin());
}
for(;i<=n;i++)
scanf("%d",&j); printf("%I64d\n",ok?f[n]:-);
}
return ;
}

POJ-3017 Cut the Sequence DP+单调队列+堆的更多相关文章

  1. poj 3017 Cut the Sequence(单调队列优化DP)

    Cut the Sequence \(solution:\) 这道题出的真的很好,奈何数据水啊! 这道题当时看得一脸懵逼,说二分也不像二分,说贪心也不像贪心,说搜索吧这题数据范围怎么这么大?而且这题看 ...

  2. POJ 3017 Cut the Sequence (单调队列优化DP)

    题意: 给定含有n个元素的数列a,要求将其划分为若干个连续子序列,使得每个序列的元素之和小于等于m,问最小化所有序列中的最大元素之和为多少?(n<=105.例:n=8, m=17,8个数分别为2 ...

  3. poj 3017 Cut the Sequence(单调队列优化 )

    题目链接:http://poj.org/problem?id=3017 题意:给你一个长度为n的数列,要求把这个数列划分为任意块,每块的元素和小于m,使得所有块的最大值的和最小 分析:这题很快就能想到 ...

  4. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  5. POJ 3017 DP + 单调队列 + 堆

    题意:给你一个长度为n的数列,你需要把这个数列分成几段,每段的和不超过m,问各段的最大值之和的最小值是多少? 思路:dp方程如下:设dp[i]为把前i个数分成合法的若干段最大值的最小值是多少.dp转移 ...

  6. POJ 3017 Cut the Sequence

    [题目链接] $O(n^2)$ 效率的 dp 递推式:${ dp }_{ i }=min\left( dp_{ j }+\overset { i }{ \underset { x=j+1 }{ max ...

  7. POJ - 3162 Walking Race 树形dp 单调队列

    POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...

  8. poj3017 Cut the Sequence[平衡树+单调队列优化]

    这里已经讲得很清楚了. 本質上是決策點與區間最大值有一定關係,於是用单调队列来维护决策集合(而不是常规的),然后在决策集合中选取最小值. 然后觉得这题方法还是很重要的.没写平衡树,用优先队列(堆)来维 ...

  9. 【POJ 2823】Sliding Window(单调队列/堆)

    BUPT2017 wintertraining(16) #5 D POJ - 2823 题意 给定n,k,求滑窗[i,i+k-1]在(1<=i<=n)的最大值最小值. 题解 单调队列或堆. ...

随机推荐

  1. FatFsVersion0.01源码分析

    目录 一.API的函数功能简述 二.FATFS主要数据结构 1.FAT32文件系统的结构 2.FATFS主要数据结构 ①   FATFS ②   DIR ③  FIL ④  FILINFO ⑤  wi ...

  2. TCO 2014 Round 1A

    顺利搞出  A B 两题,然后压线晋级了,手速场. A 题 , 求排列最小的,肯定从后往前来做,维护一个最小的set,只是第一个字母要特判一下. 1: #line 5 "EllysSorti ...

  3. Python的字符串操作和Unicode

    字符串类型 str:Unicode字符串.采用''或者r''构造的字符串均为str,单引号可以用双引号或者三引号来代替.无论用哪种方式进行制定,在Python内部存储时没有区别. bytes:二进制字 ...

  4. asp.net中下载功能

    //流方式下载 protected void ButtonButtonDownload_Click(object sender, EventArgs e) { //string fileName = ...

  5. dojo 图表制作教程

    http://www.sitepen.com/labs/code/charting/tutorial/tutorial1.html http://www.sitepen.com/labs/code/c ...

  6. BNU29368:Check the Identity(栈)

    Just determine whether an algebraic expression can always simplify to zero. Input The first line con ...

  7. RPGJS 进阶分析之 如何使用RMXP导出的数据

    首先启动RMXP 在主脚本编辑区粘贴官方提供的导出数据脚本. 之后启动并进入游戏之后按F8键就可以调出导出Map和Animation的菜单 导出后的数据保存在工程目录下的RpgJs目录下. Datab ...

  8. EJB理解

    1. 我们不禁要问,什么是"服务集群"?什么是"企业级开发"? 既然说了EJB 是为了"服务集群"和"企业级开发",那么 ...

  9. .NET之特性和属性

    1. 引言 attribute是.NET框架引入的有一技术亮点,因此我们有必要花点时间走进一个发现attribute登堂入室的入口.因为.NET Framework中使用了大量的定制特性来完成代码约定 ...

  10. find 日常使用

    find 查找文件 fidn 目录 参数 find ~ -type f find /log -name "root.log" find . -type f -exec ls -l ...