結果
| 問題 | No.771 しおり |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-10-20 15:14:37 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,149 ms / 2,000 ms |
| コード長 | 2,101 bytes |
| コンパイル時間 | 2,399 ms |
| コンパイル使用メモリ | 78,040 KB |
| 実行使用メモリ | 76,536 KB |
| 最終ジャッジ日時 | 2024-07-22 07:32:06 |
| 合計ジャッジ時間 | 18,158 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 43 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
static int[] marks;
static int[] books;
static int[][] dp;
static int n;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
n = sc.nextInt();
marks = new int[n];
books = new int[n];
for (int i = 0; i < n; i++) {
marks[i] = sc.nextInt();
books[i] = sc.nextInt();
}
dp = new int[1 << n][n];
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
ans = Math.min(ans, dfw((1 << n) - 1, i));
}
System.out.println(ans);
}
static int dfw(int mask, int idx) {
if (getPop(mask) == 1) {
return 0;
}
if (dp[mask][idx] == 0) {
dp[mask][idx] = Integer.MAX_VALUE / 2;
for (int i = 0; i < n; i++) {
if (i == idx || (mask & (1 << i)) == 0) {
continue;
}
dp[mask][idx] = Math.min(dp[mask][idx], Math.max(dfw(mask ^ (1 << idx), i), books[i] - marks[i] + marks[idx]));
}
}
return dp[mask][idx];
}
static int getPop(int x) {
int pop = 0;
while (x > 0) {
pop += x % 2;
x >>= 1;
}
return pop;
}
}
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 String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten