题目链接: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 ;
}