分析:
单调队列
d
p
dp
dp
f
i
=
m
i
n
{
f
j
}
+
a
i
f_i=min\{f_j\}+a_i
fi=min{fj}+ai
换了
d
e
q
u
e
deque
deque写法 注意开始没有元素做下标 要添个
0
0
0
CODE:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e5+5;
int n,m,f[N];
deque<int> q;
int main()
{
scanf("%d%d",&n,&m);
q.push_back(0);
for(int i=1,x;i<=n;i++)
{
scanf("%d",&x);
while(!q.empty()&&i-m>q.front()) q.pop_front();
f[i]=f[q.front()]+x;
while(!q.empty()&&f[i]<=f[q.back()]) q.pop_back();
q.push_back(i);
}
while(!q.empty()&&n-m>=q.front()) q.pop_front();
printf("%d",f[q.front()]);
return 0;
}