普及练习场 更要技巧的动规与记忆化 乌龟棋

题目链接

题意理解

代码中 dp[i][j][k][l] 表示使用了i张1,j张2,k张3,l张4能走到的最大数。

代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static int N, M;
    static int maxn = 400;
    static int[] cnt = new int[5];
    static int[] a = new int[maxn];
    static int maxm = 50;
    static int[][][][] dp = new int[maxm][maxm][maxm][maxm];
    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        N = fs.nextInt();
        M = fs.nextInt();
        for(int i = 1; i <= N; i++) {
            a[i] = fs.nextInt();
        }
        for(int i = 0; i < M; i++) {
            int x = fs.nextInt();
            cnt[x]++;
        }

        for(int i = 0; i <= cnt[1]; i++) {
            for(int j = 0; j <= cnt[2]; j++) {
                for(int k = 0; k <= cnt[3]; k++) {
                    for(int l = 0; l <= cnt[4]; l++) {
                        dp[i][j][k][l] = mmax(
                                dp[Math.max(i-1, 0)][j][k][l],
                                dp[i][Math.max(j-1, 0)][k][l],
                                dp[i][j][Math.max(k-1, 0)][l],
                                dp[i][j][k][Math.max(l-1, 0)]) + a[1 + i * 1 + j * 2 + k * 3 + l * 4];

                    }
                }
            }
        }
        System.out.println(dp[cnt[1]][cnt[2]][cnt[3]][cnt[4]]);
    }

    public static int mmax(int a, int b, int c, int d) {
        int t1 = Math.max(a, b);
        int t2 = Math.max(t1, c);
        int t3 = Math.max(t2, d);
        return t3;
    }

    public static class FastScanner {
        private BufferedReader br;
        private StringTokenizer st;
        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public String nextToken() {
            while(st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.valueOf(nextToken());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值