标签:AC pac div return include using turn tchar ==
设f[i]为i不选的最小损失,转移是f[i]=f[j]+e[i[(i-j-1<=k)
因为f是单调不降的,所以f[j]显然越靠右越好因为i-j-1<=k的限制,所以单调栈需要弹栈
#include<iostream>
#include<cstdio>
using namespace std;
const int N=100005;
int n,m,e[N],q[N],l,r;
long long f[N],ans,mn=1e18;
int read()
{
int r=0,f=1;
char p=getchar();
while(p>‘9‘||p<‘0‘)
{
if(p==‘-‘)
f=-1;
p=getchar();
}
while(p>=‘0‘&&p<=‘9‘)
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
e[i]=read(),ans+=e[i];
for(int i=1;i<=n;i++)
{
while(i-q[l]-1>m&&l<=r)
l++;
f[i]=e[i]+f[q[l]];
while(f[q[r]]>f[i]&&l<=r)
r--;
q[++r]=i;
}
for(int i=n-m;i<=n;i++)
mn=min(mn,f[i]);
printf("%lld\n",ans-mn);
return 0;
}
bzoj 2442: [Usaco2011 Open]修剪草坪【单调栈】
标签:AC pac div return include using turn tchar ==
原文地址:https://www.cnblogs.com/lokiii/p/8981773.html