結果
| 問題 | No.15 カタログショッピング |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-08-10 08:58:07 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 435 ms / 5,000 ms |
| コード長 | 4,573 bytes |
| コンパイル時間 | 2,949 ms |
| コンパイル使用メモリ | 92,896 KB |
| 実行使用メモリ | 61,032 KB |
| 最終ジャッジ日時 | 2024-11-16 10:39:40 |
| 合計ジャッジ時間 | 6,074 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
Goods.size = n;
int sum = sc.nextInt();
int[] ev = new int[n - n / 2];
int[] od = new int[n / 2];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
ev[i / 2] = sc.nextInt();
} else {
od[i / 2] = sc.nextInt();
}
}
TreeMap<Integer, ArrayList<Integer>> evMap = getMap(ev);
TreeMap<Integer, ArrayList<Integer>> odMap = getMap(od);
ArrayList<Goods> ans = new ArrayList<>();
for (int x : evMap.keySet()) {
if (x > sum) {
break;
}
if (!odMap.containsKey(sum - x)) {
continue;
}
for (int y : evMap.get(x)) {
for (int z : odMap.get(sum - x)) {
ans.add(getGoods(y, z));
}
}
}
Collections.sort(ans);
System.out.println(ans.stream().map(x -> x.toString()).collect(Collectors.joining("\n")));
}
static Goods getGoods(int ev, int od) {
Goods ans = new Goods();
for (int i = 0; ev > 0; i++) {
if (ev % 2 == 1) {
ans.buy(i * 2);
}
ev >>= 1;
}
for (int i = 0; od > 0; i++) {
if (od % 2 == 1) {
ans.buy(i * 2 + 1);
}
od >>= 1;
}
return ans;
}
static class Goods implements Comparable<Goods> {
static int size;
boolean[] exists;
public Goods() {
exists = new boolean[size];
}
public void buy(int x) {
exists[x] = true;
}
public int compareTo(Goods another) {
for (int i = 0; i < size; i++) {
if (exists[i] != another.exists[i]) {
if (exists[i]) {
return -1;
} else {
return 1;
}
}
}
return 0;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
if (exists[i]) {
if (sb.length() != 0) {
sb.append(" ");
}
sb.append(i + 1);
}
}
return sb.toString();
}
}
static TreeMap<Integer, ArrayList<Integer>> getMap(int[] arr) {
TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();
map.put(0, new ArrayList<>());
map.get(0).add(0);
for (int i = 0; i < arr.length; i++) {
Integer key = Integer.MAX_VALUE;
while ((key = map.lowerKey(key)) != null) {
if (!map.containsKey(key + arr[i])) {
map.put(key + arr[i], new ArrayList<>());
}
for (int x : map.get(key)) {
map.get(key + arr[i]).add(x + (1 << i));
}
}
}
return map;
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten