题目链接: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. python模块之os和os.path模块

    1.os模块os.listdir(dirname) 列出dirname下的目录和文件os.getcwd()函数得到当前工作目录,即当前Python脚本工作的目录路径.os.getenv()和os.pu ...

  2. Web程序发布后显示个性化图标

    采用Tomcat发布程序后,浏览器上默认显示程序的图标是Tomcat图标.如下图所示: 需要显示自己个性化的图标,比如,这里显示一个图标. 只需要如下三步设置即可. 将制作的ico图标放在程序的根目录 ...

  3. js函数文件排序化

    因为本人的某些小强迫症,写了一个格式化并根据js函数名排序的c++程序,此作mark #include <stdio.h> #include <map> #include &l ...

  4. Hive(转)

    Hive分区表 在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念.分区表指的是在创建表时指 ...

  5. WPF获取控件的句柄

    在WinForm中,获得句柄是一件很容易的事情,This.Handle或者Control.Handle就可以,最近在WPF的开发中发现找不到这个属性,一番查找资料后找到了两种方式. 1,使用Windo ...

  6. gradient css

    <!DOCTYPE html> <html> <head> <title></title> <script src="js/ ...

  7. WindowsPhone客户端第一次审核失败记录

    微软返回失败pdf,其中2.7.2和2.7.3没有通过,原因是: 1. 没有提供隐私策略,所以在提交App要填写的隐私策略url里加了隐私策略的网址链接. 2. 由于使用了定位服务,所以第一次进入应用 ...

  8. Bootstrap 与 ASP.NET MVC 4 不使用 NuGet Package 笔记

    转自 http://www.mytecbits.com/microsoft/dot-net/bootstrap-with-asp-net-mvc-4-step-by-step 单位最近做了一个Boot ...

  9. Bengio最新博文:深度学习展望

    Bengio最新博文:深度学习展望 人类一直梦想着创造有智能的机器.早在第一台可编程计算机问世前100多年,发明家就对如何能让由连杆和齿轮组成的设备也变得更加智能这一命题充满好奇.后来,20世纪40年 ...

  10. 关于hibernate的实体类中有集合类型转化成JSON的工具类 - 怀念今天的专栏 - 博客频道

    Json 来源:http://blog.csdn.net/zczzsq/article/details/18697045#1536434-hi-1-4387-42d97150898b1af15ddaa ...