結果

問題 No.1606 Stuffed Animals Keeper
ユーザー tentententen
提出日時 2023-07-18 18:52:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 534 ms / 3,000 ms
コード長 2,735 bytes
コンパイル時間 3,113 ms
コンパイル使用メモリ 85,192 KB
実行使用メモリ 64,264 KB
最終ジャッジ日時 2023-10-18 22:10:47
合計ジャッジ時間 19,397 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,444 KB
testcase_01 AC 57 ms
51,400 KB
testcase_02 AC 58 ms
52,316 KB
testcase_03 AC 447 ms
63,576 KB
testcase_04 AC 489 ms
63,684 KB
testcase_05 AC 319 ms
58,932 KB
testcase_06 AC 372 ms
63,104 KB
testcase_07 AC 454 ms
63,584 KB
testcase_08 AC 417 ms
63,792 KB
testcase_09 AC 373 ms
63,452 KB
testcase_10 AC 295 ms
60,188 KB
testcase_11 AC 206 ms
59,452 KB
testcase_12 AC 392 ms
63,804 KB
testcase_13 AC 93 ms
54,308 KB
testcase_14 AC 96 ms
54,664 KB
testcase_15 AC 99 ms
54,568 KB
testcase_16 AC 515 ms
64,080 KB
testcase_17 AC 520 ms
64,064 KB
testcase_18 AC 513 ms
64,068 KB
testcase_19 AC 529 ms
63,932 KB
testcase_20 AC 534 ms
63,900 KB
testcase_21 AC 371 ms
64,052 KB
testcase_22 AC 389 ms
64,264 KB
testcase_23 AC 362 ms
63,956 KB
testcase_24 AC 358 ms
64,052 KB
testcase_25 AC 359 ms
61,808 KB
testcase_26 AC 227 ms
59,648 KB
testcase_27 AC 249 ms
60,032 KB
testcase_28 AC 248 ms
60,188 KB
testcase_29 AC 264 ms
60,660 KB
testcase_30 AC 224 ms
59,616 KB
testcase_31 AC 214 ms
59,400 KB
testcase_32 AC 99 ms
54,820 KB
testcase_33 AC 195 ms
58,400 KB
testcase_34 AC 99 ms
54,592 KB
testcase_35 AC 189 ms
58,420 KB
testcase_36 AC 192 ms
58,308 KB
testcase_37 AC 173 ms
58,348 KB
testcase_38 AC 154 ms
58,284 KB
testcase_39 AC 98 ms
54,544 KB
testcase_40 AC 99 ms
54,312 KB
testcase_41 AC 96 ms
54,560 KB
testcase_42 AC 85 ms
54,456 KB
testcase_43 AC 389 ms
62,060 KB
testcase_44 AC 387 ms
62,156 KB
testcase_45 AC 390 ms
60,168 KB
testcase_46 AC 388 ms
62,216 KB
testcase_47 AC 402 ms
62,184 KB
testcase_48 AC 57 ms
53,444 KB
testcase_49 AC 58 ms
53,456 KB
testcase_50 AC 56 ms
53,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        ArrayList<Integer> meats = new ArrayList<>();
        ArrayList<Integer> grasses = new ArrayList<>();
        int a = 0;
        int b = 0;
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            if (x == 0) {
                a++;
            } else if (x == 1) {
                b++;
            } else {
                if (a == 0 && b == 0) {
                    continue;
                }
                meats.add(a);
                grasses.add(b);
                a = 0;
                b = 0;
            }
        }
        if (a > 0 || b > 0) {
            meats.add(a);
            grasses.add(b);
        }
        HashMap<Integer, Integer> current = new HashMap<>();
        current.put(0, 0);
        for (int i = 0; i < meats.size(); i++) {
            int x = meats.get(i);
            int y = grasses.get(i);
            HashMap<Integer, Integer> next = new HashMap<>();
            for (int z : current.keySet()) {
                next.put(z - x, Math.min(next.getOrDefault(z - x, Integer.MAX_VALUE), current.get(z)));
                next.put(z + y, Math.min(next.getOrDefault(z + y, Integer.MAX_VALUE), current.get(z) + y));
            }
            current = next;
        }
        System.out.println(current.getOrDefault(0, -1));
    }
}
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