传送门
题解:
设dp[i]表示以1~i范围内(保证i处放一个烽火台)的最小代价。
dp[i]=min{dp[j]+w[i]}(i-j+1<=m)
用一个单调递增的队列维护dp数组,每次用队首更新答案,将元素插入队尾。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e6+4;
int n,m;
int dp[MAXN];
int q[MAXN],h=1,t=0;
inline int read() {
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
int main() {
// freopen("tyvj 1313.in","r",stdin);
n=read(),m=read();
q[++t]=0;
for (register int i=1;i<=n;++i) {
while (h<=t&&i-q[h]>m) ++h;
dp[i]=dp[q[h]]+read();
while (h<=t&&dp[q[t]]>=dp[i]) --t;
q[++t]=i;
}
int ret=0x3f3f3f3f;
for (register int i=n-m+1;i<=n;++i) ret=min(ret,dp[i]);
printf("%d\n",ret);
return 0;
}