Luogu P1541 乌龟棋

gate

四 重 循 环 暴 力 枚 举

因为最终状态是用完所有的牌…所以可以根据用了多少牌来dp

设f[i][j][k][l]表示每种牌用了多少张…它就可以从f[i-1][j][k][k],f[i][j-1][k][l]...转移过来

代码如下

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#define MogeKo qwq
using namespace std;

int n,m,x,a[405],g[5],f[45][45][45][45];

int main() {
    scanf("%d%d",&n,&m);
    for(int i = 0;i < n;i++)
        scanf("%d",&a[i]);
    for(int i = 1;i <= m;i++){
        scanf("%d",&x);
        g[x]++;
    }
    f[0][0][0][0] = a[0];
    for(int i = 0;i <= g[1];i++)
    for(int j = 0;j <= g[2];j++)
    for(int k = 0;k <= g[3];k++)
    for(int l = 0;l <= g[4];l++){
        if(i) f[i][j][k][l] = max(f[i][j][k][l],f[i-1][j][k][l]+ a[i+2*j+3*k+4*l]);
        if(j) f[i][j][k][l] = max(f[i][j][k][l],f[i][j-1][k][l]+ a[i+2*j+3*k+4*l]);
        if(k) f[i][j][k][l] = max(f[i][j][k][l],f[i][j][k-1][l]+ a[i+2*j+3*k+4*l]);
        if(l) f[i][j][k][l] = max(f[i][j][k][l],f[i][j][k][l-1]+ a[i+2*j+3*k+4*l]);
    }
    printf("%d",f[g[1]][g[2]][g[3]][g[4]]);
    return 0;
}
View Code