結果

問題 No.393 2本の竹
ユーザー tenten
提出日時 2023-06-13 18:46:35
言語 Java
(openjdk 23)
結果
AC  
実行時間 332 ms / 1,000 ms
コード長 2,730 bytes
コンパイル時間 2,469 ms
コンパイル使用メモリ 89,232 KB
実行使用メモリ 133,904 KB
最終ジャッジ日時 2024-06-22 04:29:43
合計ジャッジ時間 6,247 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int n1;
    static int n2;
    static int[][] dp;
    static int[] values;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (n-- > 0) {
            n1 = sc.nextInt();
            n2 = sc.nextInt();
            values = new int[sc.nextInt()];
            for (int i = 0; i < values.length; i++) {
                values[i] = -sc.nextInt();
            }
            Arrays.sort(values);
            dp = new int[values.length][n1 + 1];
            for (int[] arr : dp) {
                Arrays.fill(arr, -1);
            }
            sb.append(dfw(values.length - 1, n1, n2)).append("\n");
        }
        System.out.print(sb);
    }
    
    static int dfw(int idx, int a, int b) {
        if (idx < 0) {
            return 0;
        }
        if (values[idx] + a < 0 && values[idx] + b < 0) {
            return 0;
        }
        if (dp[idx][a] < 0) {
            if (values[idx] + a < 0) {
                dp[idx][a] = dfw(idx - 1, a, b + values[idx]) + 1;
            } else if (values[idx] + b < 0) {
                dp[idx][a] = dfw(idx - 1, a + values[idx], b) + 1;
            }  else {
                dp[idx][a] = Math.max(dfw(idx - 1, a, b + values[idx]), dfw(idx - 1, a + values[idx], b))+ 1;
            }
        }
        return dp[idx][a];
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0