結果
問題 |
No.733 分身並列コーディング
|
ユーザー |
![]() |
提出日時 | 2021-05-12 18:08:50 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,721 bytes |
コンパイル時間 | 3,882 ms |
コンパイル使用メモリ | 80,196 KB |
実行使用メモリ | 381,600 KB |
最終ジャッジ日時 | 2024-09-23 03:09:14 |
合計ジャッジ時間 | 6,793 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 4 TLE * 1 -- * 41 |
ソースコード
import java.io.*; import java.util.*; public class Main { static ArrayList<HashMap<Integer, Integer>> dp = new ArrayList<>(); static ArrayList<Integer> list = new ArrayList<>(); static int[] sums; static int[] times; static int full; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int t = sc.nextInt(); int n = sc.nextInt(); full = (1 << n) - 1; times = new int[n]; for (int i = 0; i < n; i++) { times[i] = sc.nextInt(); } Arrays.sort(times); sums = new int[n]; sums[0] = times[0]; for (int i = 1; i < n; i++) { sums[i] = sums[i - 1] + times[i]; } make(n - 1, t, 0); for (int i = 0; i < list.size(); i++) { dp.add(new HashMap<>()); } System.out.println(dfw(list.size() - 1, 0)); } static int dfw(int idx, int mask) { if (mask == full) { return 0; } if (idx < 0) { return Integer.MAX_VALUE / 2; } if (!dp.get(idx).containsKey(mask)) { int value = dfw(idx - 1, mask); if ((mask & list.get(idx)) != list.get(idx)) { value = Math.min(value, dfw(idx - 1, mask | list.get(idx)) + 1); } dp.get(idx).put(mask, value); } return dp.get(idx).get(mask); } static void make(int idx, int remain, int value) { if (idx < 0) { list.add(value); return; } value <<= 1; if (remain < times[idx]) { make(idx - 1, remain, value); } else if (remain >= sums[idx]) { make(idx - 1, remain - times[idx], value + 1); } else { make(idx - 1, remain, value); make(idx - 1, remain - times[idx], value + 1); } } static long getCount(long x, long n) { if (x == 1 || x > 2 * n) { return 0; } else if (x <= n) { return x - 1; } else { return n - (x - n) + 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 String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }