結果
問題 | No.1606 Stuffed Animals Keeper |
ユーザー | tenten |
提出日時 | 2022-08-18 14:26:25 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 180 ms / 3,000 ms |
コード長 | 2,740 bytes |
コンパイル時間 | 2,071 ms |
コンパイル使用メモリ | 78,400 KB |
実行使用メモリ | 63,032 KB |
最終ジャッジ日時 | 2024-10-06 05:07:44 |
合計ジャッジ時間 | 9,267 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
49,980 KB |
testcase_01 | AC | 49 ms
50,192 KB |
testcase_02 | AC | 49 ms
50,008 KB |
testcase_03 | AC | 149 ms
58,468 KB |
testcase_04 | AC | 162 ms
58,692 KB |
testcase_05 | AC | 103 ms
52,936 KB |
testcase_06 | AC | 135 ms
55,924 KB |
testcase_07 | AC | 148 ms
58,528 KB |
testcase_08 | AC | 145 ms
58,652 KB |
testcase_09 | AC | 106 ms
52,900 KB |
testcase_10 | AC | 97 ms
52,860 KB |
testcase_11 | AC | 77 ms
51,224 KB |
testcase_12 | AC | 117 ms
54,900 KB |
testcase_13 | AC | 88 ms
51,740 KB |
testcase_14 | AC | 85 ms
51,484 KB |
testcase_15 | AC | 74 ms
51,556 KB |
testcase_16 | AC | 168 ms
63,032 KB |
testcase_17 | AC | 168 ms
51,976 KB |
testcase_18 | AC | 170 ms
52,096 KB |
testcase_19 | AC | 180 ms
52,100 KB |
testcase_20 | AC | 175 ms
51,944 KB |
testcase_21 | AC | 130 ms
42,036 KB |
testcase_22 | AC | 116 ms
41,180 KB |
testcase_23 | AC | 116 ms
41,304 KB |
testcase_24 | AC | 130 ms
42,024 KB |
testcase_25 | AC | 125 ms
41,992 KB |
testcase_26 | AC | 103 ms
40,024 KB |
testcase_27 | AC | 103 ms
40,048 KB |
testcase_28 | AC | 105 ms
40,072 KB |
testcase_29 | AC | 100 ms
40,048 KB |
testcase_30 | AC | 100 ms
39,604 KB |
testcase_31 | AC | 98 ms
39,536 KB |
testcase_32 | AC | 74 ms
38,812 KB |
testcase_33 | AC | 96 ms
39,640 KB |
testcase_34 | AC | 72 ms
38,856 KB |
testcase_35 | AC | 94 ms
39,620 KB |
testcase_36 | AC | 95 ms
39,112 KB |
testcase_37 | AC | 98 ms
39,156 KB |
testcase_38 | AC | 98 ms
39,188 KB |
testcase_39 | AC | 90 ms
38,872 KB |
testcase_40 | AC | 86 ms
38,452 KB |
testcase_41 | AC | 83 ms
38,808 KB |
testcase_42 | AC | 84 ms
38,948 KB |
testcase_43 | AC | 168 ms
56,112 KB |
testcase_44 | AC | 168 ms
51,964 KB |
testcase_45 | AC | 173 ms
56,180 KB |
testcase_46 | AC | 170 ms
52,060 KB |
testcase_47 | AC | 168 ms
56,176 KB |
testcase_48 | AC | 50 ms
37,044 KB |
testcase_49 | AC | 49 ms
36,932 KB |
testcase_50 | AC | 50 ms
36,944 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { static ArrayList<Animal> animals = new ArrayList<>(); static int[][] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int meat = 0; int grass = 0; int sumMeat = 0; for (int i = 0; i < n; i++) { int x = sc.nextInt(); if (x == 0) { meat++; } else if (x == 1) { grass++; } else { if (meat > 0 || grass > 0) { animals.add(new Animal(meat, grass)); sumMeat += meat; meat = 0; grass = 0; } } } if (meat > 0 || grass > 0) { animals.add(new Animal(meat, grass)); sumMeat += meat; } dp = new int[animals.size()][sumMeat + 1]; for (int[] arr : dp) { Arrays.fill(arr, -1); } int ans = dfw(animals.size() - 1, sumMeat); if (ans >= Integer.MAX_VALUE / 2) { System.out.println(-1); } else { System.out.println(ans / 2); } } static int dfw(int idx, int value) { if (value < 0) { return Integer.MAX_VALUE / 2; } if (idx < 0) { if (value == 0) { return 0; } else { return Integer.MAX_VALUE / 2; } } if (dp[idx][value] < 0) { dp[idx][value] = Math.min(dfw(idx - 1, value) + animals.get(idx).meat, dfw(idx - 1, value - animals.get(idx).sum()) + animals.get(idx).grass); } return dp[idx][value]; } static class Animal { int meat; int grass; public Animal(int meat, int grass) { this.meat = meat; this.grass = grass; } public int sum() { return meat + grass; } } } 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 double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }