結果

問題 No.50 おもちゃ箱
ユーザー jp_stejp_ste
提出日時 2016-05-14 11:15:23
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,167 bytes
コンパイル時間 2,775 ms
コンパイル使用メモリ 85,500 KB
実行使用メモリ 76,844 KB
最終ジャッジ日時 2024-04-16 06:16:35
合計ジャッジ時間 13,392 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
41,800 KB
testcase_01 WA -
testcase_02 AC 161 ms
42,144 KB
testcase_03 AC 147 ms
41,744 KB
testcase_04 AC 141 ms
41,256 KB
testcase_05 AC 142 ms
41,464 KB
testcase_06 AC 163 ms
42,092 KB
testcase_07 AC 145 ms
41,788 KB
testcase_08 AC 146 ms
41,592 KB
testcase_09 AC 148 ms
41,728 KB
testcase_10 AC 144 ms
41,708 KB
testcase_11 AC 149 ms
41,596 KB
testcase_12 AC 149 ms
41,712 KB
testcase_13 AC 145 ms
41,336 KB
testcase_14 AC 147 ms
41,580 KB
testcase_15 AC 145 ms
41,392 KB
testcase_16 AC 151 ms
41,652 KB
testcase_17 AC 195 ms
43,932 KB
testcase_18 AC 207 ms
44,544 KB
testcase_19 WA -
testcase_20 AC 160 ms
42,004 KB
testcase_21 AC 180 ms
42,476 KB
testcase_22 AC 216 ms
45,948 KB
testcase_23 AC 146 ms
41,596 KB
testcase_24 AC 135 ms
41,576 KB
testcase_25 AC 130 ms
41,464 KB
testcase_26 AC 143 ms
41,920 KB
testcase_27 AC 132 ms
40,716 KB
testcase_28 AC 212 ms
44,364 KB
testcase_29 AC 260 ms
48,192 KB
testcase_30 AC 733 ms
64,360 KB
testcase_31 AC 144 ms
41,448 KB
testcase_32 AC 248 ms
47,524 KB
testcase_33 AC 247 ms
47,476 KB
testcase_34 AC 142 ms
41,392 KB
testcase_35 AC 193 ms
43,200 KB
testcase_36 AC 245 ms
47,696 KB
testcase_37 AC 188 ms
42,988 KB
testcase_38 AC 706 ms
61,264 KB
testcase_39 AC 148 ms
41,576 KB
testcase_40 AC 161 ms
42,044 KB
testcase_41 AC 749 ms
76,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    final static Integer DEL_VALUE = new Integer(99);
    
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            int n = Integer.parseInt(scan.next());
            ArrayList<Integer> a = new ArrayList<>();
            for(int i=0; i<n; i++) {
                a.add(Integer.parseInt(scan.next()));
            }
              
            int m = Integer.parseInt(scan.next());
            ArrayList<Integer> b = new ArrayList<>();
            for(int i=0; i<m; i++) {
                b.add(Integer.parseInt(scan.next()));
            }
            
            a.sort(Collections.reverseOrder());
            b.sort(Collections.reverseOrder());
            
            LinkedList< ArrayList<Integer> > checkList = new LinkedList<>();
            LinkedList< ArrayList<Integer> > nextCheckList = new LinkedList<>();
            checkList.add(a);
            
            for(int i=0; i<b.size(); i++) {
                
                while(!checkList.isEmpty()) {
                    final ArrayList<Integer> nowList = checkList.poll();
                    ArrayList<String> p = getPermutationList(b.get(i), nowList);
                    
                    for(String flag : p) {
                        ArrayList<Integer> tmpList = (ArrayList<Integer>)nowList.clone();
                        
                        for(int j=0; j<tmpList.size(); j++) {
                            if(flag.charAt(j) == '1') {
                                tmpList.set(j, DEL_VALUE);
                            }
                        }
                        while( tmpList.indexOf(DEL_VALUE) >= 0 ) {
                            tmpList.remove(DEL_VALUE);
                        }
                        if(tmpList.size() == 0) {
                            System.out.println(i+1);
                            System.exit(0);
                        }
                        nextCheckList.add(tmpList);
                    }
                }
                
                checkList = (LinkedList< ArrayList<Integer> >)nextCheckList.clone();
                nextCheckList.clear();
            }
            System.out.println(-1);
        }        
    }
        
    static ArrayList<String> getPermutationList(final int BOX_SIZE, ArrayList<Integer> a) {
        ArrayList<String> pList = new ArrayList<>();
        
        int n = (int)Math.pow(2, a.size());
        for(int i=n-1; i>=0; i--) {
            String flag = Integer.toBinaryString(i);
            int len = (a.size()-flag.length());
            for(int j=0; j<len; j++) flag = "0" + flag;
            
            int sum = 0;
            for(int j=0; j<a.size(); j++) {
                if(flag.charAt(j) == '1') {
                    sum += (a.get(j));
                    if(sum > BOX_SIZE) break;
                }
            }
            if(sum <= BOX_SIZE) {
                pList.add(flag);
                if(pList.size() >= 5) break;
            }
        }        
        return pList;
    }
}
0