結果

問題 No.15 カタログショッピング
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-05 02:12:04
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,902 bytes
コンパイル時間 5,281 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 68,608 KB
最終ジャッジ日時 2023-08-24 19:12:15
合計ジャッジ時間 5,511 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
52,832 KB
testcase_01 AC 79 ms
52,648 KB
testcase_02 AC 81 ms
53,492 KB
testcase_03 AC 85 ms
52,748 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

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