dp【i】表示以i为结尾的合法方案最小值。
转移方程为,要么选当前值,即dp【i-1】+a【i】
要么选i-k+1到i-1中的最小值。
即f【i】=dp【i-1】+a【i】
然后单调队列维护即可。
做了两题能自己推了qaq
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#include<bitset>
#include<unordered_map>
using namespace std;
#define LL long long
#define eps (1e-9)
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const LL inf = 1e18;
const double pi = acos(-1.0);
const int bas = 131;
const LL mod = 1e6 + 3;
LL a[maxn];
LL sum[maxn], q[maxn], dp[maxn];
LL f(int x)
{
if (x == 0)return 0;
return dp[x - 1] + a[x];
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
dp[i] = inf;
}
int hh = 0, tt = 0;
for (int i = 1; i <= n; i++)
{
if (i - q[hh] >= m)hh++;
dp[i] = min(dp[i - 1] + a[i], f(q[hh]));
while (hh <= tt && f(q[tt]) >= f(i))tt--;
q[++tt] = i;
}
printf("%lld", dp[n]);
return 0;
}