Codeforces 1389 B. Array Walk 做题记录(DP)
(纯种的DP还是做得有点苦痛,调了好久。太菜了。)
大概就是第一层枚举返回几次,第二层遍历一遍$1~n$。
#include <bits/stdc++.h> using namespace std; const int maxn=1e5; int t; int n,k,z; int a[maxn+5]; int sum[maxn+5]; int main(){ scanf("%d",&t); while (t--){ scanf("%d%d%d",&n,&k,&z); int d[n+5][7][5]; // d[i][j]: the max score at the index i, with return-times j int ans=0; memset(d,0,sizeof(d)); for (int i=1;i<=n;i++){ scanf("%d",&a[i]); d[i][0][0]=d[i-1][0][0]+a[i]; if (i<=(k+1)&&d[i][0][0]>ans) ans=d[i][0][0]; } for (int j=1,lim;j<=z;j++){ lim=(k+1)-2*j; for (int i=1;i<=lim&&i<=k;i++){ if (i-1>=1) d[i][j][0]=max(d[i-1][j][0],d[i-1][j][1])+a[i]; if (i+1<=n) d[i][j][1]=d[i+1][j-1][0]+a[i]; if (d[i][j][1]>ans) ans=d[i][j][1]; if (d[i][j][0]>ans) ans=d[i][j][0]; } } printf("%d\n",ans); } }

浙公网安备 33010602011771号