結果

問題 No.1606 Stuffed Animals Keeper
ユーザー tentententen
提出日時 2021-11-29 16:02:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,071 ms / 3,000 ms
コード長 2,262 bytes
コンパイル時間 3,086 ms
コンパイル使用メモリ 75,652 KB
実行使用メモリ 216,320 KB
最終ジャッジ日時 2023-09-15 08:21:33
合計ジャッジ時間 26,917 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,320 KB
testcase_01 AC 42 ms
49,340 KB
testcase_02 AC 42 ms
49,364 KB
testcase_03 AC 702 ms
151,916 KB
testcase_04 AC 900 ms
175,948 KB
testcase_05 AC 362 ms
75,660 KB
testcase_06 AC 491 ms
97,760 KB
testcase_07 AC 703 ms
141,172 KB
testcase_08 AC 646 ms
133,136 KB
testcase_09 AC 463 ms
84,084 KB
testcase_10 AC 310 ms
67,572 KB
testcase_11 AC 202 ms
58,856 KB
testcase_12 AC 493 ms
95,636 KB
testcase_13 AC 67 ms
50,392 KB
testcase_14 AC 65 ms
50,652 KB
testcase_15 AC 65 ms
50,552 KB
testcase_16 AC 1,043 ms
216,088 KB
testcase_17 AC 1,062 ms
216,144 KB
testcase_18 AC 1,063 ms
216,320 KB
testcase_19 AC 1,061 ms
215,952 KB
testcase_20 AC 1,071 ms
216,024 KB
testcase_21 AC 515 ms
100,660 KB
testcase_22 AC 504 ms
96,656 KB
testcase_23 AC 496 ms
95,848 KB
testcase_24 AC 481 ms
95,580 KB
testcase_25 AC 501 ms
95,632 KB
testcase_26 AC 213 ms
61,152 KB
testcase_27 AC 246 ms
64,800 KB
testcase_28 AC 238 ms
64,012 KB
testcase_29 AC 256 ms
64,664 KB
testcase_30 AC 226 ms
60,844 KB
testcase_31 AC 211 ms
60,984 KB
testcase_32 AC 66 ms
50,848 KB
testcase_33 AC 169 ms
55,520 KB
testcase_34 AC 68 ms
50,576 KB
testcase_35 AC 167 ms
57,300 KB
testcase_36 AC 160 ms
57,292 KB
testcase_37 AC 155 ms
55,104 KB
testcase_38 AC 130 ms
55,384 KB
testcase_39 AC 69 ms
50,356 KB
testcase_40 AC 78 ms
50,824 KB
testcase_41 AC 65 ms
50,456 KB
testcase_42 AC 65 ms
50,308 KB
testcase_43 AC 816 ms
169,396 KB
testcase_44 AC 815 ms
171,440 KB
testcase_45 AC 820 ms
169,516 KB
testcase_46 AC 812 ms
169,352 KB
testcase_47 AC 838 ms
169,512 KB
testcase_48 AC 42 ms
49,368 KB
testcase_49 AC 43 ms
49,620 KB
testcase_50 AC 43 ms
49,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        ArrayList<Integer> meat = new ArrayList<>();
        ArrayList<Integer> bege = new ArrayList<>();
        int total = 0;
        int count = 0;
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            if (x == 2 && total > 0) {
                meat.add(count);
                bege.add(total - count);
            }
            if (x == 2) {
                count = 0;
                total = 0;
                continue;
            }
            total++;
            count += x;
        }
        if (total > 0 && count > 0 && count < total) {
            meat.add(count);
            bege.add(total - count);
        }
        int length = meat.size();
        ArrayList<HashMap<Integer, Integer>> dp = new ArrayList<>();
        for (int i = 0; i <= length; i++) {
            dp.add(new HashMap<>());
        }
        dp.get(0).put(0, 0);
        for (int i = 1; i <= length; i++) {
            int m = meat.get(i - 1);
            int b = bege.get(i - 1);
            for (int x : dp.get(i - 1).keySet()) {
                dp.get(i).put(x + m, Math.min(dp.get(i).getOrDefault(x + m, Integer.MAX_VALUE), dp.get(i - 1).get(x) + m));
                dp.get(i).put(x - b, Math.min(dp.get(i).getOrDefault(x - b, Integer.MAX_VALUE), dp.get(i - 1).get(x)));
            }
        }
        System.out.println(dp.get(length).getOrDefault(0, -1));
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0