結果
問題 | No.15 カタログショッピング |
ユーザー | mastersatoshi |
提出日時 | 2015-08-14 17:55:29 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,337 bytes |
コンパイル時間 | 2,389 ms |
コンパイル使用メモリ | 82,548 KB |
実行使用メモリ | 117,336 KB |
最終ジャッジ日時 | 2024-07-18 09:27:57 |
合計ジャッジ時間 | 7,940 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 116 ms
40,148 KB |
testcase_01 | AC | 117 ms
41,452 KB |
testcase_02 | AC | 137 ms
41,284 KB |
testcase_03 | AC | 143 ms
42,036 KB |
testcase_04 | AC | 131 ms
41,500 KB |
testcase_05 | AC | 850 ms
109,768 KB |
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 (TreeMap<Integer, Integer> set11 : set1) { merged.add(set11); } } 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)); } } ArrayList<String> answer = new ArrayList(); for (TreeMap<Integer, Integer> tmpSet : merged) { StringBuilder bf = new StringBuilder(); boolean first = true; for (int num : tmpSet.keySet()) { if (first) { bf.append(num); first = false; } else { bf.append(" ").append(num); } } answer.add(bf.toString()); } answer.sort(null); for (String str : answer) { System.out.println(str); } } private static void merger( TreeMap<Integer, Integer> merge, TreeMap<Integer, Integer> source) { for (int key : source.keySet()) { merge.put(key, source.get(key)); } } }