結果

問題 No.15 カタログショッピング
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-05 02:13:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 284 ms / 5,000 ms
コード長 2,907 bytes
コンパイル時間 2,484 ms
コンパイル使用メモリ 77,000 KB
実行使用メモリ 69,104 KB
最終ジャッジ日時 2023-08-24 19:12:22
合計ジャッジ時間 4,853 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
53,284 KB
testcase_01 AC 79 ms
52,640 KB
testcase_02 AC 83 ms
53,236 KB
testcase_03 AC 84 ms
53,840 KB
testcase_04 AC 81 ms
52,960 KB
testcase_05 AC 266 ms
68,856 KB
testcase_06 AC 282 ms
68,268 KB
testcase_07 AC 282 ms
67,860 KB
testcase_08 AC 283 ms
69,104 KB
testcase_09 AC 284 ms
68,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        int s=sc.nextInt();
        int[]p=sc.nextIntArray(n);
        int m=n/2;
        int[]f=new int[1<<m],snd=new int[1<<(n-m)];
        for(int b=0;b<1<<m;++b)
            for(int i=0;i<m;++i)
                if((b&1<<i)!=0)
                    f[b]+=p[i];
        for(int b=0;b<1<<(n-m);++b)
            for(int i=0;i<n-m;++i)
                if((b&1<<i)!=0)
                    snd[b]+=p[m+i];
        Map<Integer,List<Integer>>hm=new HashMap<Integer,List<Integer>>();
        for(int b=0;b<1<<(n-m);++b){
            if(!hm.containsKey(snd[b]))
                hm.put(snd[b],new ArrayList<Integer>());
            hm.get(snd[b]).add(b);
        }
        List<Integer>ans=new ArrayList<Integer>();
        for(int b=0;b<1<<m;++b){
            if(hm.containsKey(s-f[b])){
                for(int e:hm.get(s-f[b]))
                    ans.add(Integer.reverse((e<<m|b)<<1));
            }
        }
        Collections.sort(ans);
        Collections.reverse(ans);
        for(int e:ans){
            List<Integer>l=new ArrayList<Integer>();
            for(int i=0;i<31;++i)
                if((e&(1<<(30-i)))!=0)
                    l.add(i);
            for(int i=0;i<l.size();++i)
                out.print((l.get(i)+1)+(i==l.size()-1?"\n":" "));
        }
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
0