結果
| 問題 | No.1606 Stuffed Animals Keeper |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-29 15:56:50 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,321 bytes |
| 記録 | |
| コンパイル時間 | 3,052 ms |
| コンパイル使用メモリ | 80,092 KB |
| 実行使用メモリ | 158,080 KB |
| 最終ジャッジ日時 | 2024-07-02 12:35:53 |
| 合計ジャッジ時間 | 20,693 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 WA * 22 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
ArrayList<Integer> meat = new ArrayList<>();
ArrayList<Integer> bege = new ArrayList<>();
int total = 0;
int count = 0;
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
if (x == 2 && total > 0 && count > 0 && count < total) {
meat.add(count);
bege.add(total - count);
count = 0;
total = 0;
}
if (x == 0 || x == 1) {
total++;
}
if (x == 1) {
count++;
}
}
if (total > 0 && count > 0 && count < total) {
meat.add(count);
bege.add(total - count);
}
int length = meat.size();
ArrayList<HashMap<Integer, Integer>> dp = new ArrayList<>();
for (int i = 0; i <= length; i++) {
dp.add(new HashMap<>());
}
dp.get(0).put(0, 0);
for (int i = 1; i <= length; i++) {
int m = meat.get(i - 1);
int b = bege.get(i - 1);
for (int x : dp.get(i - 1).keySet()) {
dp.get(i).put(x + m, Math.min(dp.get(i).getOrDefault(x + m, Integer.MAX_VALUE), dp.get(i - 1).get(x) + m));
dp.get(i).put(x - b, Math.min(dp.get(i).getOrDefault(x - b, Integer.MAX_VALUE), dp.get(i - 1).get(x)));
}
}
System.out.println(dp.get(length).getOrDefault(0, -1));
}
}
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 {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten