結果
| 問題 | No.15 カタログショッピング |
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2018-03-11 12:43:12 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 56 ms / 5,000 ms |
| コード長 | 1,803 bytes |
| コンパイル時間 | 722 ms |
| コンパイル使用メモリ | 110,996 KB |
| 実行使用メモリ | 14,364 KB |
| 最終ジャッジ日時 | 2024-06-13 00:05:33 |
| 合計ジャッジ時間 | 1,540 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
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);
}
norioc