結果

問題 No.393 2本の竹
ユーザー tenten
提出日時 2021-01-21 11:04:08
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,841 bytes
コンパイル時間 2,156 ms
コンパイル使用メモリ 82,224 KB
実行使用メモリ 123,828 KB
最終ジャッジ日時 2024-12-24 09:40:50
合計ジャッジ時間 14,411 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

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