結果

問題 No.50 おもちゃ箱
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-11-15 18:01:39
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 160 ms / 5,000 ms
コード長 1,896 bytes
コンパイル時間 1,332 ms
コンパイル使用メモリ 149,316 KB
実行使用メモリ 6,012 KB
最終ジャッジ日時 2023-09-02 22:54:33
合計ジャッジ時間 4,151 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 58 ms
6,012 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 1 ms
4,368 KB
testcase_05 AC 23 ms
4,528 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 1 ms
4,368 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 1 ms
4,372 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 1 ms
4,368 KB
testcase_14 AC 1 ms
4,372 KB
testcase_15 AC 1 ms
4,372 KB
testcase_16 AC 2 ms
4,368 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 7 ms
4,368 KB
testcase_20 AC 1 ms
4,368 KB
testcase_21 AC 1 ms
4,368 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 1 ms
4,368 KB
testcase_24 AC 1 ms
4,372 KB
testcase_25 AC 1 ms
4,372 KB
testcase_26 AC 1 ms
4,368 KB
testcase_27 AC 6 ms
4,372 KB
testcase_28 AC 26 ms
4,368 KB
testcase_29 AC 25 ms
4,372 KB
testcase_30 AC 19 ms
4,368 KB
testcase_31 AC 1 ms
4,372 KB
testcase_32 AC 65 ms
4,372 KB
testcase_33 AC 31 ms
4,368 KB
testcase_34 AC 160 ms
4,376 KB
testcase_35 AC 34 ms
4,372 KB
testcase_36 AC 44 ms
4,368 KB
testcase_37 AC 32 ms
4,372 KB
testcase_38 AC 27 ms
4,372 KB
testcase_39 AC 51 ms
4,372 KB
testcase_40 AC 45 ms
4,372 KB
testcase_41 AC 42 ms
4,368 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(30): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`
Main.d(35): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`
Main.d(41): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`

ソースコード

diff #

import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

void log(A...)(A arg) {
    stderr.writeln(arg);
}
int size(T)(in T s) {
    return cast(int)s.length;
}

void main() {
    int N; readf("%d\n", &N);
    auto A = readln.chomp.split(" ").map!(to!int).array;
    int M; readf("%d\n", &M);
    auto B = readln.chomp.split(" ").map!(to!int).array;

    auto pos = new int[][M];
    for (int bit = 0; bit < (1<<N); bit++) {
        int sum = 0;
        foreach (int i, a; A) {
            if (bit & (1 << i)) {
                sum += a;
            }
        }
        foreach (int i, b; B) {
            if (b >= sum) {
                pos[i] ~= bit;
            }
        }
    }
    foreach (int i, b; B) {
        auto del = new bool[pos[i].size];
        foreach (j, p; pos[i]) {
            foreach (k, q; pos[i]) {
                if (j == k) continue;
                if ((p & q) == q) {
                    del[k] = true;
                }
            }
        }
        int[] npos;
        foreach (j, p; pos[i]) {
            if (del[j]) continue;
            npos ~= p;
        }
        pos[i] = npos;
    }

    auto dp = new bool[][](1<<N, 1<<M);
    dp[0][0] = true;
    for (int a = 0; a < (1<<N); a++) {
        for (int b = 0; b < (1<<M); b++) {
            if (! dp[a][b]) continue;
            for (int j = 0; j < M; j++) {
                if (b & (1 << j)) continue;
                foreach (p; pos[j]) {
                    dp[a | p][b | (1<<j)] = true;
                }
            }
        }
    }
    int ans = 40;
    for (int b = 0; b < (1<<M); b++) {
        if (dp[(1<<N) - 1][b]) {
            ans = min(ans, popcnt(b));
        }
    }
    if (ans == 40) ans = -1;
    writeln(ans);

}
0