【BZOJ2442】修建草坪(动态规划,单调队列)
【BZOJ2442】修建草坪(动态规划,单调队列)
题面
题解
设\(f[i]\)表示前\(i\)个里面选出来的最大值
转移应该比较显然
枚举一个断点的位置,转移一下就好
\(f[i]=max(f[j-1]+s[j]-s[i])\)
所以可以单调队列优化一下
(不优化用各种玄学可以拿90分。。。)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 120000
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
ll s[MAX],f[MAX],ans;
int n,K,h,t=1;
ll Q[MAX];
ll Get(int id)
{
ll x=f[id-1]-s[id];
while(h<=t&&x>f[Q[t]-1]-s[Q[t]])--t;
Q[++t]=id;
while(h<=t&&id-Q[h]>K)++h;
return f[Q[h]-1]-s[Q[h]];
}
int main()
{
n=read();K=read();
for(int i=1;i<=n;++i)s[i]=read()+s[i-1];
for(int i=1;i<=n;++i)ans=max(ans,f[i]=Get(i)+s[i]);
printf("%lld\n",ans);
return 0;
}