就是个单调队列+DP嘛。
——代码
#include <cstdio> const int MAXN = ;
int n, m, h = , t = , ans = ~( << );
int q[MAXN], a[MAXN], f[MAXN]; inline int min(int x, int y)
{
return x < y ? x : y;
} int main()
{
int i;
scanf("%d %d", &n, &m);
for(i = ; i <= n; i++) scanf("%d", &a[i]);
for(i = ; i <= n; i++)
{
while(h <= t && q[h] < i - m) h++;
f[i] = f[q[h]] + a[i];
while(h <= t && f[q[t]] > f[i]) t--;
q[++t] = i;
}
for(i = n - m + ; i <= n; i++) ans = min(ans, f[i]);
printf("%d\n", ans);
return ;
}