結果

問題 No.393 2本の竹
ユーザー tentententen
提出日時 2021-01-21 11:04:08
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,841 bytes
コンパイル時間 2,525 ms
コンパイル使用メモリ 86,124 KB
実行使用メモリ 197,648 KB
最終ジャッジ日時 2023-08-25 22:25:35
合計ジャッジ時間 7,858 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
56,152 KB
testcase_01 AC 123 ms
58,128 KB
testcase_02 AC 148 ms
55,872 KB
testcase_03 AC 156 ms
62,048 KB
testcase_04 AC 123 ms
56,152 KB
testcase_05 AC 121 ms
55,976 KB
testcase_06 AC 133 ms
56,092 KB
testcase_07 AC 163 ms
59,564 KB
testcase_08 AC 186 ms
58,652 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int d = sc.nextInt();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < d; i++) {
		    int n1 = sc.nextInt();
		    int n2 = sc.nextInt();
		    int m = sc.nextInt();
		    int[] customers = new int[m];
		    for (int j = 0; j < m; j++) {
		        customers[j] = sc.nextInt();
		    }
		    Arrays.sort(customers);
		    if (customers[0] > n1 && customers[0] > n2) {
		        sb.append(0).append("\n");
		        continue;
		    }
		    ArrayList<HashMap<Integer, HashMap<Integer, Boolean>>> dp = new ArrayList<>();
		    for (int j = 0; j < m; j++) {
		        dp.add(new HashMap<>());
		    }
		    int left = 0;
		    int right = m;
		    while (right - left > 1) {
		        int x = (left + right) / 2;
		        if (dfw(x, n1, n2, customers, dp)) {
		            left = x;
		        } else {
		            right = x;
		        }
		    }
		    sb.append(right).append("\n");
		}
	    System.out.print(sb);
	}
	
	static boolean dfw(int idx, int small, int large, int[] customers, ArrayList<HashMap<Integer, HashMap<Integer, Boolean>>> dp) {
	    if (small < 0 || large < 0) {
	        return false;
	    } 
	    if (idx < 0) {
	        return true;
	    }
	    if (small > large) {
	        return dfw(idx, large, small, customers, dp);
	    }
	    if (dp.get(idx).containsKey(small)) {
	        if (dp.get(idx).get(small).containsKey(large)) {
	            return dp.get(idx).get(small).get(large);
	        }
	    } else {
	        dp.get(idx).put(small, new HashMap<>());
	    }
	    boolean next = (dfw(idx - 1, small - customers[idx], large, customers, dp) || dfw(idx - 1, small, large - customers[idx], customers, dp)) ;
	    dp.get(idx).get(small).put(large, next);
	    return next;
	}
}
0