結果

問題 No.15 カタログショッピング
ユーザー tentententen
提出日時 2023-08-10 08:58:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 455 ms / 5,000 ms
コード長 4,573 bytes
コンパイル時間 2,897 ms
コンパイル使用メモリ 86,524 KB
実行使用メモリ 72,300 KB
最終ジャッジ日時 2024-04-28 02:25:00
合計ジャッジ時間 6,355 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
50,984 KB
testcase_01 AC 72 ms
51,008 KB
testcase_02 AC 75 ms
51,104 KB
testcase_03 AC 80 ms
50,760 KB
testcase_04 AC 74 ms
50,552 KB
testcase_05 AC 446 ms
70,904 KB
testcase_06 AC 449 ms
60,848 KB
testcase_07 AC 437 ms
72,300 KB
testcase_08 AC 455 ms
60,988 KB
testcase_09 AC 441 ms
61,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0