結果

問題 No.1606 Stuffed Animals Keeper
ユーザー tentententen
提出日時 2023-04-25 19:14:05
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,139 bytes
コンパイル時間 3,061 ms
コンパイル使用メモリ 85,888 KB
実行使用メモリ 596,084 KB
最終ジャッジ日時 2024-04-26 23:25:50
合計ジャッジ時間 28,878 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,180 KB
testcase_01 AC 55 ms
50,016 KB
testcase_02 AC 55 ms
50,540 KB
testcase_03 AC 2,745 ms
382,184 KB
testcase_04 TLE -
testcase_05 AC 893 ms
135,352 KB
testcase_06 AC 1,558 ms
221,052 KB
testcase_07 AC 2,708 ms
360,180 KB
testcase_08 TLE -
testcase_09 AC 1,511 ms
176,252 KB
testcase_10 AC 749 ms
112,324 KB
testcase_11 AC 273 ms
65,120 KB
testcase_12 AC 1,768 ms
207,244 KB
testcase_13 AC 80 ms
51,724 KB
testcase_14 AC 92 ms
51,648 KB
testcase_15 AC 89 ms
51,644 KB
testcase_16 MLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
権限があれば一括ダウンロードができます

ソースコード

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 ArrayList<HashMap<Integer, HashMap<Integer, Integer>>> dp = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int mCount = 0;
        int gCount = 0;
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            if (x == 0) {
                mCount++;
            } else if (x == 1) {
                gCount++;
            } else {
                if (mCount > 0 || gCount > 0) {
                    meats.add(mCount);
                    grasses.add(gCount);
                    dp.add(new HashMap<>());
                    mCount = 0;
                    gCount = 0;
                }
            }
        }
        if (mCount > 0 || gCount > 0) {
            meats.add(mCount);
            grasses.add(gCount);
            dp.add(new HashMap<>());
        }
        int ans = dfw(dp.size() - 1, 0, 0);
        if (ans > n * 2) {
            System.out.println(-1);
        } else {
            System.out.println(ans / 2);
        }
    }
    
    static int dfw(int idx, int m, int g) {
        if (idx < 0) {
            if (m == 0 && g == 0) {
                return 0;
            } else {
                return Integer.MAX_VALUE / 2;
            }
        }
        if (!dp.get(idx).containsKey(m)) {
            dp.get(idx).put(m, new HashMap<>());
        }
        if (!dp.get(idx).get(m).containsKey(g)) {
            dp.get(idx).get(m).put(g, Math.min(dfw(idx - 1, m - grasses.get(idx), g + grasses.get(idx)) + grasses.get(idx), dfw(idx - 1, m + meats.get(idx), g - meats.get(idx)) + meats.get(idx)));
        }
        return dp.get(idx).get(m).get(g);
    }
}
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