結果
| 問題 | No.1861 Required Number |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-03-07 10:15:04 |
| 言語 | Java (openjdk 23) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,574 bytes |
| 記録 | |
| コンパイル時間 | 2,537 ms |
| コンパイル使用メモリ | 79,476 KB |
| 実行使用メモリ | 544,092 KB |
| 最終ジャッジ日時 | 2024-07-22 03:14:06 |
| 合計ジャッジ時間 | 7,571 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | MLE * 1 -- * 45 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
static ArrayList<HashMap<Integer, HashSet<Integer>>> dp = new ArrayList<>();
static int[] values;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int k = sc.nextInt();
values = new int[n];
for (int i = 0; i < n; i++) {
dp.add(new HashMap<>());
values[i] = sc.nextInt();
}
HashSet<Integer> ans = dfw(n - 1, k);
if (ans == null) {
System.out.println(-1);
} else {
System.out.println(ans.size());
}
}
static HashSet<Integer> dfw(int idx, int count) {
if (count == 0) {
return new HashSet<>();
}
if (count < 0 || idx < 0) {
return null;
}
if (!dp.get(idx).containsKey(count)) {
HashSet<Integer> ans1 = dfw(idx - 1, count);
HashSet<Integer> ans2 = dfw(idx - 1, count - values[idx]);
if (ans1 == null) {
if (ans2 == null) {
dp.get(idx).put(count, null);
} else {
HashSet<Integer> ans = new HashSet<>();
ans.addAll(ans2);
ans.add(idx);
dp.get(idx).put(count, ans);
}
} else {
if (ans2 == null) {
dp.get(idx).put(count, ans1);
} else {
HashSet<Integer> ans = new HashSet<>();
for (int x : ans1) {
if (ans2.contains(x)) {
ans.add(x);
}
}
dp.get(idx).put(count, ans);
}
}
}
return dp.get(idx).get(count);
}
}
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