luogu1541 乌龟棋 (dp)
dp..dp的时候不能设f[N][x1][x2][x3][x4],会T,要把N省略,然后通过1/2/3/4牌的数量来算已经走到哪一个了
1 #include<bits/stdc++.h> 2 #define pa pair<int,int> 3 #define ll long long 4 using namespace std; 5 const int maxn=360,maxm=44; 6 7 inline ll rd(){ 8 ll x=0;char c=getchar();int neg=1; 9 while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();} 10 while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); 11 return x*neg; 12 } 13 14 int N,M,m[5]; 15 int f[maxm][maxm][maxm][maxm]; 16 bool flag[6][5][maxm]; 17 int v[maxn]; 18 19 inline void sm(int &a,int b){ 20 if(b>a) a=b; 21 } 22 23 int main(){ 24 int i,j,k; 25 N=rd(),M=rd(); 26 for(i=0;i<N;i++) v[i]=rd(); 27 for(i=1;i<=M;i++) m[rd()]++; 28 f[0][0][0][0]=v[0]; 29 for(int x1=0;x1<=m[1];x1++){ 30 for(int x2=0;x2<=m[2];x2++){ 31 for(int x3=0;x3<=m[3];x3++){ 32 for(int x4=0;x4<=m[4];x4++){ 33 int now=f[x1][x2][x3][x4],pos=x1*1+x2*2+x3*3+x4*4; 34 if(x1<m[1]) sm(f[x1+1][x2][x3][x4],now+v[pos+1]); 35 if(x2<m[2]) sm(f[x1][x2+1][x3][x4],now+v[pos+2]); 36 if(x3<m[3]) sm(f[x1][x2][x3+1][x4],now+v[pos+3]); 37 if(x4<m[4]) sm(f[x1][x2][x3][x4+1],now+v[pos+4]); 38 } 39 } 40 } 41 }printf("%d\n",f[m[1]][m[2]][m[3]][m[4]]); 42 return 0; 43 }