背景:
h
e
h
e
.
.
.
hehe...
hehe...
题目传送门:
https://www.luogu.org/problemnew/show/P3800
题意:
当前这一格
x
x
x可以从上一行
[
x
−
t
,
x
+
t
]
[x-t,x+t]
[x−t,x+t]转移过来,走到最后一行的最大值。
思路:
套路题。
注意:
d
p
dp
dp转移放在最后(防止
h
e
a
d
+
+
head++
head++后清空整个队列使转移错误)。
代码:
// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,q,t,ans=0;
int a[4010][4010],f[2][4010],que[4010];
int main()
{
int x,y,z,op=0;
scanf("%d %d %d %d",&n,&m,&q,&t);
for(int i=1;i<=q;i++)
{
scanf("%d %d %d",&x,&y,&z);
a[x][y]=z;
}
for(int i=1;i<=n;i++)
{
int head=1,tail=0;
for(int j=1;j<=min(t,m);j++)
{
while(head<=tail&&f[op^1][j]>=f[op^1][que[tail]]) tail--;
que[++tail]=j;
}
for(int j=1;j<=m;j++)
{
if(j+t<=m)
{
while(head<=tail&&f[op^1][j+t]>=f[op^1][que[tail]]) tail--;
que[++tail]=j+t;
}
while(head<=tail&&j-que[head]>t) head++;
f[op][j]=f[op^1][que[head]]+a[i][j];
}
op^=1;
}
for(int j=1;j<=m;j++)
ans=max(ans,f[op^1][j]);
printf("%d",ans);
}