1 条题解

  • 0
    @ 2021-6-15 1:40:02

    C :

    #include <stdio.h>
    #define N 500
    int q[N],map[N],s[45][45][45][45]={0};
    int max(int a,int b)
    {if(a>b) return a;
     else return b;
     
        }
    int main()
    {
        int i,j,l,k,n,m,a=0,b=0,c=0,d=0;
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        scanf("%d",&map[i]);
        
        
        for(i=0;i<m;i++)
        {scanf("%d",&q[i]);
         if(q[i]==1)a++;
         if(q[i]==2)b++;
         if(q[i]==3)c++;
         if(q[i]==4)d++;}
         s[a][b][c][d]=map[0];
         for(i=a;i>=0;i--){
            for(j=b;j>=0;j--){
               for(l=c;l>=0;l--){
                 for(k=d;k>=0;k--){
                   s[i][j][l][k]=max(s[i][j][l][k],s[i+1][j][l][k]+map[1*(a-i)+2*(b-j)+3*(c-l)+4*(d-k)]);
                   s[i][j][l][k]=max(s[i][j][l][k],s[i][j+1][l][k]+map[1*(a-i)+2*(b-j)+3*(c-l)+4*(d-k)]);
                   s[i][j][l][k]=max(s[i][j][l][k],s[i][j][l+1][k]+map[1*(a-i)+2*(b-j)+3*(c-l)+4*(d-k)]);
                   s[i][j][l][k]=max(s[i][j][l][k],s[i][j][l][k+1]+map[1*(a-i)+2*(b-j)+3*(c-l)+4*(d-k)]);
                 
                 }
               }
            }
         }
         printf("%d",s[0][0][0][0]);
        
        return 0;}
    
    

    C++ :

    #include <iostream>
    #define MAX(A,B) ((A)>(B)?(A):(B))
    using namespace std;
    int T[1000];
    int card[5] = {0};
    int DP[40][40][40][40] = {0};
    
    int main()
    {
            int N,M,t;
            cin >> N >> M;
            for(int i=1; i<=N; ++i)
                    cin >> T[i];
            for(int i=0; i<M; ++i)
            {
                    cin >> t;
                    ++card[t];
            }
            DP[0][0][0][0] = T[1];
            for(int a=0; a<=card[1]; ++a)
                    for(int b=0; b<=card[2]; ++b)
                            for(int c=0; c<=card[3]; ++c)
                                    for(int d=0; d<=card[4]; ++d)
            {
                    int t = T[1*a+2*b+3*c+4*d+1];
                    if(a>0) DP[a][b][c][d] = MAX(DP[a-1][b][c][d]+t,DP[a][b][c][d]);
                    if(b>0) DP[a][b][c][d] = MAX(DP[a][b-1][c][d]+t,DP[a][b][c][d]);
                    if(c>0) DP[a][b][c][d] = MAX(DP[a][b][c-1][d]+t,DP[a][b][c][d]);
                    if(d>0) DP[a][b][c][d] = MAX(DP[a][b][c][d-1]+t,DP[a][b][c][d]);
            }
            cout << DP[card[1]][card[2]][card[3]][card[4]] << endl;
            return 0;
    }
    
    

    Pascal :

    var
       f:array[-1..41,-1..41,-1..41,-1..41]of longint;
       p,a,b:array[0..10000]of longint;
       i,n,m,j,k,l:longint;
    begin
     readln(n,m);
     fillchar(p,sizeof(p),0);
     for i:=1 to n do read(a[i]);
     for i:=1 to m do
      begin
       read(b[i]);
       inc(p[b[i]]);
      end;
    // f[0,0,0,0]:=a[1];
     for i:=0 to p[1] do
      for j:=0 to p[2] do
       for k:=0 to p[3] do
        for l:=0 to p[4] do
         begin
          if f[i-1,j,k,l]+a[i+j*2+k*3+l*4+1]>f[i,j,k,l]
           then f[i,j,k,l]:=f[i-1,j,k,l]+a[i+j*2+k*3+l*4+1];
          if f[i,j-1,k,l]+a[i+j*2+k*3+l*4+1]>f[i,j,k,l]
           then f[i,j,k,l]:=f[i,j-1,k,l]+a[i+j*2+k*3+l*4+1];
          if f[i,j,k-1,l]+a[i+j*2+k*3+l*4+1]>f[i,j,k,l]
           then f[i,j,k,l]:=f[i,j,k-1,l]+a[i+j*2+k*3+l*4+1];
          if f[i,j,k,l-1]+a[i+j*2+k*3+l*4+1]>f[i,j,k,l]
           then f[i,j,k,l]:=f[i,j,k,l-1]+a[i+j*2+k*3+l*4+1];
         end;
     writeln(f[p[1],p[2],p[3],p[4]]);
    end.
    
    • 1

    信息

    ID
    281
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者