結果

問題 No.15 カタログショッピング
ユーザー te-shte-sh
提出日時 2016-09-26 15:14:47
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 988 bytes
コンパイル時間 887 ms
コンパイル使用メモリ 127,380 KB
実行使用メモリ 10,336 KB
最終ジャッジ日時 2023-09-02 22:20:44
合計ジャッジ時間 1,942 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 45 ms
9,856 KB
testcase_06 AC 45 ms
9,600 KB
testcase_07 AC 44 ms
9,844 KB
testcase_08 AC 45 ms
10,316 KB
testcase_09 AC 44 ms
10,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;

void main()
{
  auto rd = readln.split;
  auto n = rd[0].to!size_t, s = rd[1].to!int;
  auto pi = iota(n).map!(_ => readln.chomp.to!int).array;

  if (n == 1) {
    if (pi[0] == s) writeln(1);
    else writeln(-1);
    return;
  }

  auto m1 = n / 2;
  auto m2 = n - m1;

  size_t[][int] hi;
  foreach (i; 0..1 << m2) {
    auto u = i.bitsSet.map!(j => pi[j + m1]).sum;
    hi[u] ~= i;
  }

  size_t[] ri;
  foreach (i; 0..1 << m1) {
    auto u = i.bitsSet.map!(j => pi[j]).sum;
    auto w = s - u;
    if (w < 0 || !(w in hi)) continue;
    ri ~= hi[w].map!(x => (x << m1) | i).array;
  }

  if (ri.empty) {
    writeln(-1);
  } else {
    auto ri2 = ri.map!(r => r.bitsSet.array).array;
    ri2.sort();
    ri2.each!(r => writeln(r.map!("a + 1").map!(to!string).join(" ")));
  }
}
0