結果

問題 No.15 カタログショッピング
ユーザー noriocnorioc
提出日時 2018-03-11 12:43:12
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 63 ms / 5,000 ms
コード長 1,803 bytes
コンパイル時間 791 ms
コンパイル使用メモリ 104,092 KB
実行使用メモリ 15,524 KB
最終ジャッジ日時 2023-09-03 18:59:47
合計ジャッジ時間 1,959 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 60 ms
13,372 KB
testcase_06 AC 61 ms
14,920 KB
testcase_07 AC 62 ms
15,524 KB
testcase_08 AC 63 ms
13,988 KB
testcase_09 AC 62 ms
15,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

void calc(int[] ps, int s) {

    int[][int] count(int[] xs) {
        int[][int] d;
        for (int i = 1; i < (1 << xs.length); i++) {
            int sum = 0;
            for (int j = 0; j < xs.length; j++) {
                if (i & (1 << j))
                    sum += xs[j];
            }

            d[sum] ~= i;
        }
        return d;
    }

    auto lps = ps[0 .. ps.length / 2];
    auto rps = ps[ps.length / 2 .. $];

    auto lmap = count(lps);
    auto rmap = count(rps);

    void print(int l, int r, ref int[][] ans) {
        int[] buf;
        for (int i = 0; i < lps.length; i++) {
            if (l & (1 << i))
                buf ~= i + 1;
        }
        for (int i = 0; i < rps.length; i++) {
            if (r & (1 << i))
                buf ~= cast(int)lps.length + i + 1;
        }
        ans ~= buf;
    }

    int[][] ans;
    if (s in lmap) {
        foreach (d; lmap[s]) print(d, 0, ans);
    }
    if (s in rmap) {
        foreach (d; rmap[s]) print(0, d, ans);
    }

    foreach (k, v; lmap) {
        if (k < s && (s - k) in rmap) {
            foreach (a; lmap[k]) {
                foreach (b; rmap[s - k]) {
                    print(a, b, ans);
                }
            }
        }
    }

    ans.sort();
    foreach (a; ans) {
        writeln(a.map!(e => e.to!string).join(" "));
    }
}

void main() {
    auto ns = readints;
    int n = ns[0], s = ns[1];

    int[] ps;
    for (int i = 0; i < n; i++) {
        ps ~= readint;
    }

    calc(ps, s);
}
0