結果

問題 No.15 カタログショッピング
ユーザー 夕叢霧香(ゆうむらきりか)
提出日時 2018-01-05 02:13:13
言語 Java
(openjdk 23)
結果
AC  
実行時間 320 ms / 5,000 ms
コード長 2,907 bytes
コンパイル時間 2,993 ms
コンパイル使用メモリ 87,336 KB
実行使用メモリ 57,364 KB
最終ジャッジ日時 2024-12-23 04:05:52
合計ジャッジ時間 5,572 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

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