結果

問題 No.1606 Stuffed Animals Keeper
ユーザー tentententen
提出日時 2023-03-25 11:54:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 367 ms / 3,000 ms
コード長 2,990 bytes
コンパイル時間 4,131 ms
コンパイル使用メモリ 85,348 KB
実行使用メモリ 69,964 KB
最終ジャッジ日時 2023-10-18 23:12:31
合計ジャッジ時間 13,222 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,464 KB
testcase_01 AC 56 ms
53,468 KB
testcase_02 AC 56 ms
53,472 KB
testcase_03 AC 187 ms
62,276 KB
testcase_04 AC 195 ms
62,204 KB
testcase_05 AC 114 ms
56,112 KB
testcase_06 AC 133 ms
58,212 KB
testcase_07 AC 188 ms
62,164 KB
testcase_08 AC 186 ms
62,048 KB
testcase_09 AC 142 ms
56,544 KB
testcase_10 AC 106 ms
55,748 KB
testcase_11 AC 88 ms
54,976 KB
testcase_12 AC 142 ms
58,924 KB
testcase_13 AC 95 ms
54,600 KB
testcase_14 AC 94 ms
54,588 KB
testcase_15 AC 86 ms
54,516 KB
testcase_16 AC 208 ms
66,748 KB
testcase_17 AC 211 ms
66,740 KB
testcase_18 AC 209 ms
66,692 KB
testcase_19 AC 205 ms
66,672 KB
testcase_20 AC 210 ms
66,644 KB
testcase_21 AC 138 ms
58,704 KB
testcase_22 AC 150 ms
57,056 KB
testcase_23 AC 147 ms
57,368 KB
testcase_24 AC 153 ms
58,896 KB
testcase_25 AC 140 ms
58,888 KB
testcase_26 AC 115 ms
56,352 KB
testcase_27 AC 119 ms
56,088 KB
testcase_28 AC 117 ms
56,264 KB
testcase_29 AC 117 ms
56,240 KB
testcase_30 AC 115 ms
56,144 KB
testcase_31 AC 117 ms
55,728 KB
testcase_32 AC 86 ms
54,620 KB
testcase_33 AC 114 ms
56,240 KB
testcase_34 AC 96 ms
54,588 KB
testcase_35 AC 109 ms
55,576 KB
testcase_36 AC 96 ms
55,552 KB
testcase_37 AC 104 ms
55,264 KB
testcase_38 AC 107 ms
54,800 KB
testcase_39 AC 97 ms
54,512 KB
testcase_40 AC 85 ms
54,616 KB
testcase_41 AC 113 ms
54,600 KB
testcase_42 AC 114 ms
54,328 KB
testcase_43 AC 218 ms
66,872 KB
testcase_44 AC 212 ms
69,704 KB
testcase_45 AC 217 ms
69,964 KB
testcase_46 AC 218 ms
69,776 KB
testcase_47 AC 367 ms
66,612 KB
testcase_48 AC 122 ms
52,936 KB
testcase_49 AC 80 ms
52,936 KB
testcase_50 AC 119 ms
50,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<Integer> meats = new ArrayList<>();
    static ArrayList<Integer> grasses = new ArrayList<>();
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int mCount = 0;
        int gCount = 0;
        int gSum = 0;
        for (int i = 0; i < n; i++) {
            int type = sc.nextInt();
            if (type == 0) {
                mCount++;
            } else if (type == 1) {
                gCount++;
                gSum++;
            } else {
                if (mCount > 0 || gCount > 0) {
                    meats.add(mCount);
                    grasses.add(gCount);
                    mCount = 0;
                    gCount = 0;
                }
            }
        }
        if (mCount > 0 || gCount > 0) {
            meats.add(mCount);
            grasses.add(gCount);
        }
        dp = new int[meats.size()][gSum + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        int ans = dfw(meats.size() - 1, gSum);
        if (ans >= Integer.MAX_VALUE / 2) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    static int dfw(int idx, int v) {
        if (v < 0) {
            return Integer.MAX_VALUE / 2;
        }
        if (v == 0) {
            return 0;
        } 
        if (idx < 0) {
            return Integer.MAX_VALUE / 2;
        }
        if (dp[idx][v] < 0) {
            dp[idx][v] = Math.min(dfw(idx - 1, v), dfw(idx - 1, v - meats.get(idx) - grasses.get(idx)) + meats.get(idx));
        }
        return dp[idx][v];
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0