修剪草坪
https://www.acwing.com/problem/content/description/1089/
https://class.51nod.com/Html/Textbook/ChapterIndex.html#textbookId=126&chapterId=337
由于是连续的会罢工,所以每次我们考虑的就是连续一段奶牛工作。
如果 \(f_i\) 表示最大效率,需要求前缀和。
但是如果 \(f_i\) 表示 \(i\) 罢工时前 \(i\) 的最小损失,那么只需要枚举工作段,然后加上损失。
注意不超过 \(k\) 不会罢工,所以是 \((i-k-1,i)\)。
需要分配完所有的,所以不能只对 \(f\) 取 \(min\),最后可以加一个空的位置 \(n+1\)。
#include<iostream>
using namespace std;
const int N=100010;
int n,k,a[N],q[N];
typedef long long ll;
ll sum,f[N],ans=1e18;
int main(){
#ifdef LOCAL
freopen("1.txt","r",stdin);
#endif
#ifndef LOCAL
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
#endif
cin>>n>>k;
for(int i=1;i<=n;++i)cin>>a[i],sum+=a[i];
int hh=0,tt=0;
for(int i=1;i<=n+1;++i){
while(hh<=tt&&q[hh]<i-k-1)++hh;
f[i]=a[i]+f[q[hh]];
while(hh<=tt&&f[q[tt]]>=f[i])--tt;
q[++tt]=i;
}
cout<<sum-f[n+1];
return 0;
}