結果
問題 | No.15 カタログショッピング |
ユーザー | mastersatoshi |
提出日時 | 2015-08-14 17:42:02 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,121 bytes |
コンパイル時間 | 2,382 ms |
コンパイル使用メモリ | 88,028 KB |
実行使用メモリ | 117,048 KB |
最終ジャッジ日時 | 2024-07-18 09:27:28 |
合計ジャッジ時間 | 8,791 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 129 ms
41,328 KB |
testcase_01 | AC | 140 ms
41,412 KB |
testcase_02 | AC | 155 ms
41,496 KB |
testcase_03 | AC | 153 ms
42,244 KB |
testcase_04 | AC | 140 ms
41,560 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int n1 = n / 2; int n2 = n - n1; int s = sc.nextInt(); int[] p1 = new int[n1]; int[] p2 = new int[n2]; for (int i = 0; i < n1; i++) { p1[i] = sc.nextInt(); } for (int i = 0; i < n2; i++) { p2[i] = sc.nextInt(); } TreeMap<Integer, ArrayList<TreeMap<Integer, Integer>>> fir = new TreeMap(); TreeMap<Integer, ArrayList<TreeMap<Integer, Integer>>> sec = new TreeMap(); int max1 = (int) Math.pow(2, p1.length); int max2 = (int) Math.pow(2, p2.length); for (int i = 1; i < max1; i++) { char[] bit = Integer.toBinaryString(i).toCharArray(); int sum = 0; TreeMap<Integer, Integer> tmpSet = new TreeMap(); for (int j = 0; j < bit.length; j++) { if (bit[j] == '1') { sum = sum + p1[bit.length - 1 - j]; tmpSet.put(bit.length - j, p1[bit.length - 1 - j]); } } ArrayList<TreeMap<Integer, Integer>> tmpList; if (fir.containsKey(sum)) { tmpList = fir.get(sum); } else { tmpList = new ArrayList(); fir.put(sum, tmpList); } tmpList.add(tmpSet); } for (int i = 1; i < max2; i++) { char[] bit = Integer.toBinaryString(i).toCharArray(); int sum = 0; TreeMap<Integer, Integer> tmpSet = new TreeMap(); for (int j = 0; j < bit.length; j++) { if (bit[j] == '1') { sum = sum + p2[bit.length - 1 - j]; tmpSet.put(bit.length - j + n1, p2[bit.length - 1 - j]); } } ArrayList<TreeMap<Integer, Integer>> tmpList; if (sec.containsKey(sum)) { tmpList = sec.get(sum); } else { tmpList = new ArrayList(); sec.put(sum, tmpList); } tmpList.add(tmpSet); } ArrayList<TreeMap<Integer, Integer>> merged = new ArrayList(); if (fir.containsKey(s)) { ArrayList<TreeMap<Integer, Integer>> set1 = fir.get(s); for (int i = 0; i < set1.size(); i++) { merged.add(set1.get(i)); } } for (int key : fir.keySet()) { if (!sec.containsKey(s - key)) { continue; } ArrayList<TreeMap<Integer, Integer>> set1 = fir.get(key); ArrayList<TreeMap<Integer, Integer>> set2 = sec.get(s - key); for (int i = 0; i < set1.size(); i++) { TreeMap<Integer, Integer> merge = new TreeMap(); merger(merge, set1.get(i)); for (int j = 0; j < set2.size(); j++) { TreeMap<Integer, Integer> tmp = (TreeMap)merge.clone(); merger(tmp, set2.get(j)); merged.add(tmp); } } } if (sec.containsKey(s)) { ArrayList<TreeMap<Integer, Integer>> set2 = sec.get(s); for (int i = 0; i < set2.size(); i++) { merged.add(set2.get(i)); } } for (TreeMap<Integer, Integer> tmpSet : merged) { boolean first = true; for (int num : tmpSet.keySet()) { if (first) { System.out.print(num); first = false; } else { System.out.print(" " + num); } } System.out.println(); } } private static void merger( TreeMap<Integer, Integer> merge, TreeMap<Integer, Integer> source) { for (int key : source.keySet()) { merge.put(key, source.get(key)); } } }