結果

問題 No.50 おもちゃ箱
ユーザー jp_stejp_ste
提出日時 2016-05-23 05:40:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,046 ms / 5,000 ms
コード長 4,663 bytes
コンパイル時間 4,622 ms
コンパイル使用メモリ 86,756 KB
実行使用メモリ 134,724 KB
最終ジャッジ日時 2023-09-08 04:02:13
合計ジャッジ時間 15,598 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
55,904 KB
testcase_01 AC 590 ms
81,816 KB
testcase_02 AC 137 ms
56,772 KB
testcase_03 AC 131 ms
55,760 KB
testcase_04 AC 125 ms
55,824 KB
testcase_05 AC 146 ms
55,800 KB
testcase_06 AC 149 ms
55,536 KB
testcase_07 AC 124 ms
55,872 KB
testcase_08 AC 125 ms
55,748 KB
testcase_09 AC 126 ms
55,428 KB
testcase_10 AC 122 ms
56,100 KB
testcase_11 AC 126 ms
55,856 KB
testcase_12 AC 122 ms
55,420 KB
testcase_13 AC 122 ms
55,628 KB
testcase_14 AC 127 ms
55,804 KB
testcase_15 AC 123 ms
55,752 KB
testcase_16 AC 129 ms
55,832 KB
testcase_17 AC 121 ms
56,000 KB
testcase_18 AC 120 ms
55,704 KB
testcase_19 AC 633 ms
88,500 KB
testcase_20 AC 130 ms
55,924 KB
testcase_21 AC 167 ms
59,740 KB
testcase_22 AC 178 ms
60,128 KB
testcase_23 AC 121 ms
55,756 KB
testcase_24 AC 121 ms
56,100 KB
testcase_25 AC 122 ms
55,688 KB
testcase_26 AC 137 ms
53,992 KB
testcase_27 AC 173 ms
59,988 KB
testcase_28 AC 165 ms
59,480 KB
testcase_29 AC 170 ms
59,652 KB
testcase_30 AC 126 ms
55,424 KB
testcase_31 AC 124 ms
56,036 KB
testcase_32 AC 275 ms
67,764 KB
testcase_33 AC 1,046 ms
134,724 KB
testcase_34 AC 163 ms
59,936 KB
testcase_35 AC 210 ms
62,488 KB
testcase_36 AC 409 ms
74,212 KB
testcase_37 AC 167 ms
56,388 KB
testcase_38 AC 167 ms
59,556 KB
testcase_39 AC 218 ms
61,596 KB
testcase_40 AC 844 ms
115,608 KB
testcase_41 AC 167 ms
58,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main2 {
    static int n, m;
    static ArrayList<Integer> a, b;
    
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            n = Integer.parseInt(scan.next());
            a = new ArrayList<>();
            for(int i=0; i<n; i++) {
                a.add(Integer.parseInt(scan.next()));
            }
              
            m = Integer.parseInt(scan.next());
            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();
                    
                    boolean flag = false;
                    for(Integer v : nowList) {
                        if(b.get(i) < v) {
                            flag = true;
                            break;
                        }
                    }
                    if(flag) continue;
                    
                    nodeList.clear();
                    solve(0, b.get(i), new ArrayList<Integer>(), nowList);
                    
                    for(int k=0; k<nodeList.size(); k++) {
                        ArrayList<Integer> pList = nodeList.get(k).list;
                        ArrayList<Integer> tmpList = (ArrayList<Integer>)nowList.clone();
                        
                        for(int j=0; j<pList.size(); j++) {
                            tmpList.remove(pList.get(j));
                        }
                        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<Node> nodeList = new ArrayList<>();
    
    static void solve(int i, int box, ArrayList<Integer> list, ArrayList<Integer> a) {
        ArrayList<Integer> tmpList = (ArrayList<Integer>)list.clone();
        
        if(i == a.size()) {
            Node node = new Node(box, (ArrayList<Integer>)list.clone());
            
            for(Node now : nodeList) {
                if(node.equals(now)) {
                    return;
                }
            }
            nodeList.add(node);
            return;
        }
        
        solve(i+1, box, tmpList, a);
        
        if((box - a.get(i)) >= 0) {
            tmpList.add(a.get(i));
            solve(i+1, box-a.get(i), tmpList, a);
        }
    }
    
    static class Node implements Comparable<Node> {
        int rem;
        ArrayList<Integer> list;
        
        Node(int rem, ArrayList<Integer> list) {
            this.rem = rem;
            this.list = list;
        }
        @Override
        public int compareTo(Node o) {
            if(rem == o.rem) {
                if(list.size() == o.list.size()) {
                    for(int i=0; i<list.size(); i++) {
                        if(list.get(i) == o.list.get(i)) continue;
                        return o.list.get(i) - list.get(i);
                    }
                    return 0;
                } else {
                    return list.size() - o.list.size();
                }
            } else {
                return rem - o.rem;
            }
        }
        @Override
        public String toString() {
            return rem + " " + list.toString();
        }
        @Override
        public boolean equals(Object obj) {
            Node t = (Node)obj;
            boolean flag = true;
            
            if(list.size() != t.list.size()) {
                flag = false;
            } else {
                for(int i=0; i<list.size(); i++) {
                    if(list.get(i) != t.list.get(i)) {
                        flag = false;
                        break;
                    }
                }
            }
            return flag;
        }
    }
}
0