結果

問題 No.1606 Stuffed Animals Keeper
ユーザー tentententen
提出日時 2021-07-20 19:44:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 99 ms / 3,000 ms
コード長 2,257 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 76,228 KB
実行使用メモリ 54,620 KB
最終ジャッジ日時 2023-09-24 12:54:15
合計ジャッジ時間 8,233 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,248 KB
testcase_01 AC 45 ms
49,788 KB
testcase_02 AC 45 ms
49,300 KB
testcase_03 AC 91 ms
52,740 KB
testcase_04 AC 92 ms
52,660 KB
testcase_05 AC 73 ms
51,008 KB
testcase_06 AC 84 ms
51,136 KB
testcase_07 AC 89 ms
52,644 KB
testcase_08 AC 81 ms
51,296 KB
testcase_09 AC 77 ms
51,224 KB
testcase_10 AC 72 ms
50,992 KB
testcase_11 AC 59 ms
47,976 KB
testcase_12 AC 82 ms
51,124 KB
testcase_13 AC 69 ms
50,452 KB
testcase_14 AC 76 ms
50,464 KB
testcase_15 AC 76 ms
50,948 KB
testcase_16 AC 94 ms
52,668 KB
testcase_17 AC 99 ms
53,060 KB
testcase_18 AC 96 ms
52,412 KB
testcase_19 AC 94 ms
52,396 KB
testcase_20 AC 94 ms
52,724 KB
testcase_21 AC 84 ms
51,156 KB
testcase_22 AC 80 ms
51,000 KB
testcase_23 AC 84 ms
52,624 KB
testcase_24 AC 83 ms
51,292 KB
testcase_25 AC 80 ms
51,544 KB
testcase_26 AC 75 ms
51,512 KB
testcase_27 AC 77 ms
51,144 KB
testcase_28 AC 76 ms
51,120 KB
testcase_29 AC 75 ms
50,972 KB
testcase_30 AC 72 ms
51,300 KB
testcase_31 AC 74 ms
51,216 KB
testcase_32 AC 79 ms
50,340 KB
testcase_33 AC 72 ms
50,828 KB
testcase_34 AC 66 ms
50,520 KB
testcase_35 AC 72 ms
51,868 KB
testcase_36 AC 70 ms
50,828 KB
testcase_37 AC 70 ms
50,748 KB
testcase_38 AC 70 ms
50,564 KB
testcase_39 AC 78 ms
51,400 KB
testcase_40 AC 68 ms
50,812 KB
testcase_41 AC 67 ms
50,900 KB
testcase_42 AC 67 ms
51,380 KB
testcase_43 AC 95 ms
52,576 KB
testcase_44 AC 97 ms
52,692 KB
testcase_45 AC 97 ms
52,400 KB
testcase_46 AC 95 ms
52,824 KB
testcase_47 AC 99 ms
54,620 KB
testcase_48 AC 44 ms
49,460 KB
testcase_49 AC 45 ms
49,296 KB
testcase_50 AC 44 ms
49,400 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();
        int zeroCount = 0;
        int oneCount = 0;
        ArrayList<Integer> zeros = new ArrayList<>();
        ArrayList<Integer> ones = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            if (x == 2) {
                if (zeroCount > 0 || oneCount > 0) {
                    zeros.add(zeroCount);
                    ones.add(oneCount);
                    zeroCount = 0;
                    oneCount = 0;
                }
            } else if (x == 0) {
                zeroCount++;
            } else {
                oneCount++;
            }
        }
        if (zeroCount > 0 || oneCount > 0) {
            zeros.add(zeroCount);
            ones.add(oneCount);
        }
        int total = 0;
        int size = zeros.size();
        for (int x : zeros) {
            total += x;
        }
        int[] dp = new int[total + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 0; i < size; i++) {
            int sum = zeros.get(i) + ones.get(i);
            int value = ones.get(i);
            for (int j = total; j - sum >= 0; j--) {
                if (dp[j - sum] == Integer.MAX_VALUE) {
                    continue;
                }
                dp[j] = Math.min(dp[j], dp[j - sum] + value);
            }
        }
        if (dp[total] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(dp[total]);
        }
    }
}
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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0