結果

問題 No.50 おもちゃ箱
ユーザー htensaihtensai
提出日時 2020-01-29 15:28:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 1,933 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 74,664 KB
実行使用メモリ 51,016 KB
最終ジャッジ日時 2023-10-14 05:43:43
合計ジャッジ時間 6,142 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,404 KB
testcase_01 AC 46 ms
49,452 KB
testcase_02 AC 44 ms
49,436 KB
testcase_03 AC 43 ms
49,428 KB
testcase_04 AC 43 ms
49,388 KB
testcase_05 AC 49 ms
49,284 KB
testcase_06 AC 45 ms
49,588 KB
testcase_07 AC 44 ms
49,368 KB
testcase_08 AC 44 ms
49,352 KB
testcase_09 AC 44 ms
49,444 KB
testcase_10 AC 44 ms
49,296 KB
testcase_11 AC 44 ms
50,000 KB
testcase_12 AC 44 ms
49,420 KB
testcase_13 AC 43 ms
49,412 KB
testcase_14 AC 44 ms
49,348 KB
testcase_15 AC 45 ms
49,592 KB
testcase_16 AC 44 ms
49,288 KB
testcase_17 AC 45 ms
49,644 KB
testcase_18 AC 45 ms
49,404 KB
testcase_19 AC 47 ms
49,216 KB
testcase_20 AC 47 ms
49,316 KB
testcase_21 AC 46 ms
49,940 KB
testcase_22 AC 45 ms
49,348 KB
testcase_23 AC 44 ms
49,488 KB
testcase_24 AC 44 ms
49,476 KB
testcase_25 AC 43 ms
49,480 KB
testcase_26 AC 44 ms
49,544 KB
testcase_27 AC 46 ms
49,588 KB
testcase_28 AC 46 ms
49,588 KB
testcase_29 AC 46 ms
49,436 KB
testcase_30 AC 47 ms
49,308 KB
testcase_31 AC 44 ms
49,496 KB
testcase_32 AC 47 ms
49,428 KB
testcase_33 AC 45 ms
49,412 KB
testcase_34 AC 56 ms
51,016 KB
testcase_35 AC 46 ms
49,276 KB
testcase_36 AC 46 ms
49,440 KB
testcase_37 AC 45 ms
49,564 KB
testcase_38 AC 46 ms
49,552 KB
testcase_39 AC 46 ms
49,272 KB
testcase_40 AC 46 ms
49,884 KB
testcase_41 AC 45 ms
49,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int n;
    static int[] toys;
    static int m;
    static int[] boxes;
    static final int MAX = 20;
    static int[][][] dp;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        toys = new int[n];
        String[] first = br.readLine().split(" ", n);
        for (int i = 0; i < n; i++) {
            toys[i] = Integer.parseInt(first[i]);
        }
        m = Integer.parseInt(br.readLine());
        boxes = new int[m];
        String[] second = br.readLine().split(" ", m);
        for (int i = 0; i < m; i++) {
            boxes[i] = Integer.parseInt(second[i]);
        }
        Arrays.sort(boxes);
        dp = new int[m][MAX + 1][1 << n];
        int value = dfw(m - 1, boxes[m - 1], (1 << n) - 1) + 1;
        if (value > m) {
            System.out.println(-1);
        } else {
            System.out.println(value);
        }
    }
    
    static int dfw(int b, int size, int key) {
        if (b < 0) {
            return Integer.MAX_VALUE / 10;
        }
        if (key == 0) {
            return 0;
        }
        if (dp[b][size][key] != 0) {
            return dp[b][size][key];
        }
        int min = Integer.MAX_VALUE;
        int count = 0;
        for (int i = 0; i < n; i++) {
            int x = 1 << i;
            if ((x & key) == 0) {
                continue;
            }
            if (toys[i] > size) {
                continue;
            }
            count++;
            min = Math.min(min, dfw(b, size - toys[i], key ^ x));
        }
        if (count == 0) {
            if (b < 1) {
                return Integer.MAX_VALUE / 10;
            }
            min = dfw(b - 1, boxes[b - 1], key) + 1;
        }
        dp[b][size][key] = min;
        return min;
    }
}
0