結果
問題 | No.15 カタログショッピング |
ユーザー | yuki2006 |
提出日時 | 2015-02-09 16:04:51 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 304 ms / 5,000 ms |
コード長 | 3,959 bytes |
コンパイル時間 | 4,155 ms |
コンパイル使用メモリ | 90,484 KB |
実行使用メモリ | 61,988 KB |
最終ジャッジ日時 | 2024-06-23 16:22:58 |
合計ジャッジ時間 | 6,963 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 132 ms
54,156 KB |
testcase_01 | AC | 134 ms
54,240 KB |
testcase_02 | AC | 135 ms
53,976 KB |
testcase_03 | AC | 140 ms
54,144 KB |
testcase_04 | AC | 134 ms
54,176 KB |
testcase_05 | AC | 285 ms
60,780 KB |
testcase_06 | AC | 284 ms
60,688 KB |
testcase_07 | AC | 304 ms
61,140 KB |
testcase_08 | AC | 298 ms
61,988 KB |
testcase_09 | AC | 280 ms
60,608 KB |
ソースコード
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Yuki015 { public static void main(String[] args) { Yuki015 p = new Yuki015(); } public class Ans extends ArrayList<Integer> implements Comparable<ArrayList<Integer>> { @Override public int compareTo(ArrayList<Integer> o) { for (int i = 0; i < Math.min(this.size(), o.size()); i++) { int comp = Integer.compare(get(i), o.get(i)); if (comp != 0) { return comp; } } return Integer.compare(size(), o.size()); } @Override public String toString() { StringBuilder ans = new StringBuilder(); for (int i = 0; i < size(); i++) { if (ans.length() > 0) { ans.append(" "); } ans.append(get(i)); } return ans.toString(); } } public class HanbunTuple implements Comparable<HanbunTuple> { public long value; public long bit; HanbunTuple(long value, long bit) { this.value = value; this.bit = bit; } @Override public int compareTo(HanbunTuple o) { return Long.compare(this.value, o.value); } } public class Tuple<S, T> { S a; T b; public Tuple(S a, T b) { this.a = a; this.b = b; } } public Yuki015() { Scanner scanner = new Scanner(System.in); final int N = scanner.nextInt(); final int S = scanner.nextInt(); int[] P = new int[N]; for (int i = 0; i < N; i++) { P[i] = scanner.nextInt(); } int sizeL = N / 2; ArrayList<HanbunTuple> leftList = new ArrayList<HanbunTuple>(2 << sizeL); ArrayList<HanbunTuple> rightList = new ArrayList<HanbunTuple>(2 << sizeL); leftList.add(new HanbunTuple(0, 0)); for (int i = 0; i < sizeL; i++) { int size = leftList.size(); for (int j = 0; j < size; j++) { HanbunTuple tuple = leftList.get(j); leftList.add(new HanbunTuple(tuple.value + P[i], tuple.bit | 1 << i)); } } rightList.add(new HanbunTuple(0, 0)); for (int i = sizeL; i < N; i++) { int size = rightList.size(); for (int j = 0; j < size; j++) { HanbunTuple tuple = rightList.get(j); rightList.add(new HanbunTuple(tuple.value + P[i], tuple.bit | 1 << i)); } } Collections.sort(rightList); ArrayList<Ans> anses = new ArrayList<>(); for (HanbunTuple tuple : leftList) { final int index = Collections.binarySearch(rightList, tuple, new Comparator<HanbunTuple>() { @Override public int compare(HanbunTuple o1, HanbunTuple o2) { return Long.compare(o1.value, S - o2.value); } }); if (index < 0) { continue; } for (int rightIndex = index; rightIndex < rightList.size(); rightIndex++) { if (rightList.get(rightIndex).value + tuple.value != S) { break; } Ans list = new Ans(); for (long bitIndex = 0; bitIndex < N; bitIndex++) { if ((rightList.get(rightIndex).bit + tuple.bit & 1 << bitIndex) > 0) { //インデックスが1ずれるため list.add((int) bitIndex + 1); } } anses.add(list); } } Collections.sort(anses); for (Ans ans : anses) { System.out.println(ans); } } }