結果

問題 No.50 おもちゃ箱
ユーザー yun_appyun_app
提出日時 2016-05-13 16:40:25
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,573 bytes
コンパイル時間 2,748 ms
コンパイル使用メモリ 91,296 KB
実行使用メモリ 50,896 KB
最終ジャッジ日時 2024-04-17 22:51:32
合計ジャッジ時間 5,955 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
50,844 KB
testcase_01 WA -
testcase_02 AC 62 ms
50,624 KB
testcase_03 AC 63 ms
50,732 KB
testcase_04 AC 72 ms
50,740 KB
testcase_05 AC 62 ms
50,724 KB
testcase_06 AC 62 ms
50,660 KB
testcase_07 AC 65 ms
50,428 KB
testcase_08 AC 62 ms
50,556 KB
testcase_09 AC 62 ms
50,700 KB
testcase_10 AC 61 ms
50,836 KB
testcase_11 AC 70 ms
50,684 KB
testcase_12 AC 60 ms
50,340 KB
testcase_13 AC 60 ms
50,644 KB
testcase_14 AC 61 ms
37,240 KB
testcase_15 AC 60 ms
37,360 KB
testcase_16 AC 60 ms
37,012 KB
testcase_17 AC 58 ms
37,340 KB
testcase_18 AC 58 ms
37,308 KB
testcase_19 AC 61 ms
37,524 KB
testcase_20 WA -
testcase_21 AC 58 ms
37,444 KB
testcase_22 AC 60 ms
37,476 KB
testcase_23 AC 59 ms
37,140 KB
testcase_24 AC 59 ms
37,420 KB
testcase_25 AC 59 ms
37,528 KB
testcase_26 AC 60 ms
37,464 KB
testcase_27 AC 57 ms
37,620 KB
testcase_28 AC 59 ms
37,176 KB
testcase_29 AC 62 ms
37,128 KB
testcase_30 AC 59 ms
37,504 KB
testcase_31 AC 61 ms
37,356 KB
testcase_32 AC 61 ms
37,420 KB
testcase_33 AC 62 ms
37,548 KB
testcase_34 AC 60 ms
37,332 KB
testcase_35 AC 61 ms
37,572 KB
testcase_36 AC 59 ms
37,568 KB
testcase_37 AC 59 ms
37,228 KB
testcase_38 AC 59 ms
37,540 KB
testcase_39 AC 60 ms
37,456 KB
testcase_40 AC 57 ms
37,404 KB
testcase_41 AC 59 ms
37,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* package whatever; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        ArrayList<Integer> toy = new ArrayList<Integer>();
        int toy_sum = 0;
        for(String st:br.readLine().split(" ")){
            int t = Integer.parseInt(st);
            toy.add(t);
            toy_sum += t;
        }
        Collections.sort(toy,(a,b)->{return b-a;});
        int m = Integer.parseInt(br.readLine());
        ArrayList<Integer> box = new ArrayList<Integer>();
        for(String st:br.readLine().split(" ")){
            box.add(Integer.parseInt(st));
        }
        Collections.sort(box,(a,b)->{return b-a;});
        int min = 0;
        int max = m;
        int ans = -1;
        while(max>=min){
            int ave = (min+max)/2;
            if(check(new ArrayList<Integer>(box.subList(0,ave)),new ArrayList<Integer>(toy),toy_sum)){
                ans = ave;
                max = ave-1;
            }else{
                min = ave+1;
            }
        }
        System.out.println(ans);
    }
    public static boolean check(ArrayList<Integer> box,ArrayList<Integer> toy,int toy_sum){
        for(int b:box){
            toy_sum-=b;
        }
        if(toy_sum>0){
            return false;
        }
        for(int i=0;i<toy.size();i++){
            int idx = box.indexOf(toy.get(i));
            if(idx >= 0){
                toy.remove(i);
                i--;
                box.set(idx,0);
            }
        }
        Collections.sort(box,(a,b)->{return b-a;});
        int box_size = box.size();
        int toy_size = toy.size();
        start:for(int j=0;j<box_size;j++){
            ArrayList<Integer> t_box = new ArrayList<Integer>(box);
            for(int i=0;i<toy_size;i++){
                int bo = t_box.get(0);
                int n = toy.get((i+j)%toy_size);
                int idx = t_box.indexOf(n);
                if(idx >= 0){
                    t_box.set(idx,0);
                }else if(bo>=n){
                    t_box.set(0,bo-n);
                }else{
                    continue start;
                }
                Collections.sort(t_box,(a,b)->{return b-a;});
            }
            return true;
        }
        return false;
    }

}
0