結果
問題 | No.1606 Stuffed Animals Keeper |
ユーザー | tenten |
提出日時 | 2023-07-18 18:52:48 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 452 ms / 3,000 ms |
コード長 | 2,735 bytes |
コンパイル時間 | 2,269 ms |
コンパイル使用メモリ | 92,484 KB |
実行使用メモリ | 49,904 KB |
最終ジャッジ日時 | 2024-09-18 18:11:40 |
合計ジャッジ時間 | 14,952 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
36,664 KB |
testcase_01 | AC | 48 ms
36,648 KB |
testcase_02 | AC | 51 ms
37,000 KB |
testcase_03 | AC | 349 ms
49,076 KB |
testcase_04 | AC | 374 ms
49,280 KB |
testcase_05 | AC | 241 ms
45,868 KB |
testcase_06 | AC | 314 ms
49,104 KB |
testcase_07 | AC | 361 ms
49,056 KB |
testcase_08 | AC | 329 ms
49,084 KB |
testcase_09 | AC | 285 ms
48,408 KB |
testcase_10 | AC | 231 ms
45,016 KB |
testcase_11 | AC | 170 ms
45,068 KB |
testcase_12 | AC | 302 ms
48,988 KB |
testcase_13 | AC | 70 ms
38,340 KB |
testcase_14 | AC | 69 ms
38,556 KB |
testcase_15 | AC | 78 ms
38,432 KB |
testcase_16 | AC | 441 ms
49,608 KB |
testcase_17 | AC | 452 ms
49,664 KB |
testcase_18 | AC | 434 ms
49,360 KB |
testcase_19 | AC | 427 ms
49,444 KB |
testcase_20 | AC | 414 ms
49,428 KB |
testcase_21 | AC | 316 ms
49,904 KB |
testcase_22 | AC | 304 ms
49,820 KB |
testcase_23 | AC | 276 ms
49,656 KB |
testcase_24 | AC | 278 ms
49,864 KB |
testcase_25 | AC | 281 ms
49,324 KB |
testcase_26 | AC | 186 ms
45,188 KB |
testcase_27 | AC | 197 ms
45,868 KB |
testcase_28 | AC | 195 ms
45,416 KB |
testcase_29 | AC | 216 ms
46,204 KB |
testcase_30 | AC | 185 ms
45,688 KB |
testcase_31 | AC | 183 ms
45,852 KB |
testcase_32 | AC | 81 ms
38,456 KB |
testcase_33 | AC | 153 ms
44,052 KB |
testcase_34 | AC | 84 ms
38,256 KB |
testcase_35 | AC | 154 ms
44,332 KB |
testcase_36 | AC | 152 ms
44,460 KB |
testcase_37 | AC | 141 ms
43,624 KB |
testcase_38 | AC | 128 ms
43,504 KB |
testcase_39 | AC | 75 ms
38,300 KB |
testcase_40 | AC | 84 ms
38,672 KB |
testcase_41 | AC | 80 ms
38,556 KB |
testcase_42 | AC | 71 ms
38,324 KB |
testcase_43 | AC | 324 ms
47,860 KB |
testcase_44 | AC | 322 ms
47,916 KB |
testcase_45 | AC | 322 ms
48,136 KB |
testcase_46 | AC | 316 ms
47,848 KB |
testcase_47 | AC | 327 ms
47,960 KB |
testcase_48 | AC | 44 ms
37,276 KB |
testcase_49 | AC | 43 ms
36,916 KB |
testcase_50 | AC | 44 ms
37,148 KB |
ソースコード
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(); } }