题意
给定一串数列,要求把它划分成一些小段,每个小段的和不超过M,找到一种分段方法使得每一段的最大值的和最小,求这个最小值
分析
易得转移方程
其中
AC代码
//POJ 3017 Cut the Sequence
//AC 2017-1-16 10:03:12
//DP, monotonic Queue, BBT
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <list>
#include <sstream>
#include <stack>
using namespace std;
#define cls(x) memset(x,0,sizeof x)
#define inf(x) memset(x,0x3f,sizeof x)
#define neg(x) memset(x,-1,sizeof x)
#define ninf(x) memset(x,0xc0,sizeof x)
#define st0(x) memset(x,false,sizeof x)
#define st1(x) memset(x,true,sizeof x)
#define lowbit(x) x&(-x)
#define input(x) scanf("%d",&(x))
#define inputt(x,y) scanf("%d %d",&(x),&(y))
#define bug cout<<"here"<<endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")//stack expansion
//#define debug
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;//1061109567-2147483647
const long long LINF=0x3f3f3f3f3f3f3f3f;//4557430888798830399-9223372036854775807
const int maxn=1e5+100;
int N;
long long M,sum;
long long num[maxn],dp[maxn];
int monque[maxn],s,e,beg;
multiset<long long> optset;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
#ifdef debug
freopen("E:\\Documents\\code\\input.txt","r",stdin);
freopen("E:\\Documents\\code\\output.txt","w",stdout);
#endif
//IO
while(scanf("%d %lld",&N,&M)!=EOF)
{
for(int i=0;i<N;++i)
scanf("%lld",num+i);
s=e=beg=0;
sum=0;
optset.clear();
for(int i=0;i<N;++i)
{
if(num[i]>M)
{
dp[N-1]=-1;
break;
}
sum+=num[i];
while(sum>M)
sum-=num[beg++];
while(s<e&&num[monque[e-1]]<=num[i])
{
if(s<e-1)
optset.erase(dp[monque[e-2]]+num[monque[e-1]]);
--e;
}
monque[e++]=i;
if(s<e-1)
optset.insert(dp[monque[e-2]]+num[monque[e-1]]);
while(s<e&&monque[s]<beg)
{
if(s<e-1)
optset.erase(dp[monque[s]]+num[monque[s+1]]);
++s;
}
dp[i]=dp[beg-1]+num[monque[s]];
if(s<e-1)
dp[i]=min(dp[i],*optset.begin());
}
printf("%lld\n",dp[N-1]);
}
return 0;
}