题解:P12932 [NOISG 2020 Prelim] Visiting Singapore
蓝到绿。
分析
题意是说如果选
注意到
那么答案就是
事实上,没参加过活动的值一定是
卡空间。需要滚动数组。
代码
const int N=1005,M=5005,inf=1e18;
int K,n,m,A,B;
int V[N],S[M],T[M];
int f[2][M][2][2];
il void solve(){
memset(f,-0x3f,sizeof(f));
K=rd,n=rd,m=rd,A=rd,B=rd;
for(re int i=1;i<=K;++i) V[i]=rd;
for(re int i=1;i<=n;++i) S[i]=rd;
for(re int i=1;i<=m;++i) T[i]=rd;
f[0][0][1][1]=0;
int w=0,res=-inf;
for(re int i=1;i<=n;++i){
w^=1;
for(re int j=0;j<=m;++j)
for(re int x=0;x<=1;++x)
for(re int y=0;y<=1;++y) f[w][j][x][y]=-inf;
for(re int j=1;j<=m;++j){
f[w][j][1][1]=max({f[w^1][j][1][1]+B,f[w][j-1][1][1]+B,f[w^1][j][0][1]+A+B,f[w][j-1][1][0]+A+B});
f[w][j][0][1]=max({f[w][j-1][0][1]+B,f[w][j-1][0][0]+A+B});
f[w][j][1][0]=max({f[w^1][j][1][0]+B,f[w^1][j][0][0]+A+B});
if(S[i]==T[j]) f[w][j][0][0]=max({f[w^1][j-1][1][1],f[w^1][j-1][0][1],f[w^1][j-1][1][0],f[w^1][j-1][0][0],0ll+((j-1)?A+B*(j-1):0)})+V[T[j]];
}
for(re int x=0;x<=1;++x)
for(re int y=0;y<=1;++y) res=max(res,f[w][m][x][y]);
}
cout<<max(res,A+B*m);
return ;
}