結果
| 問題 | No.1606 Stuffed Animals Keeper |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-04-25 19:14:05 |
| 言語 | Java (openjdk 23) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,139 bytes |
| 記録 | |
| コンパイル時間 | 3,611 ms |
| コンパイル使用メモリ | 87,912 KB |
| 実行使用メモリ | 1,083,692 KB |
| 最終ジャッジ日時 | 2024-11-14 15:29:58 |
| 合計ジャッジ時間 | 72,521 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 MLE * 1 |
| other | AC * 38 TLE * 5 MLE * 5 |
ソースコード
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();
}
}
tenten