結果
問題 | No.527 ナップサック容量問題 |
ユーザー | tenten |
提出日時 | 2023-03-07 15:41:23 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 159 ms / 2,000 ms |
コード長 | 3,037 bytes |
コンパイル時間 | 2,188 ms |
コンパイル使用メモリ | 84,840 KB |
実行使用メモリ | 64,848 KB |
最終ジャッジ日時 | 2024-09-18 02:09:00 |
合計ジャッジ時間 | 6,825 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 47 ms
36,660 KB |
testcase_01 | AC | 48 ms
36,796 KB |
testcase_02 | AC | 47 ms
37,300 KB |
testcase_03 | AC | 48 ms
37,044 KB |
testcase_04 | AC | 47 ms
36,900 KB |
testcase_05 | AC | 77 ms
42,048 KB |
testcase_06 | AC | 154 ms
55,012 KB |
testcase_07 | AC | 122 ms
50,816 KB |
testcase_08 | AC | 74 ms
41,224 KB |
testcase_09 | AC | 48 ms
37,396 KB |
testcase_10 | AC | 141 ms
54,700 KB |
testcase_11 | AC | 101 ms
46,136 KB |
testcase_12 | AC | 86 ms
42,452 KB |
testcase_13 | AC | 159 ms
55,596 KB |
testcase_14 | AC | 82 ms
41,216 KB |
testcase_15 | AC | 91 ms
46,520 KB |
testcase_16 | AC | 52 ms
37,300 KB |
testcase_17 | AC | 88 ms
41,996 KB |
testcase_18 | AC | 95 ms
46,592 KB |
testcase_19 | AC | 49 ms
37,172 KB |
testcase_20 | AC | 78 ms
40,484 KB |
testcase_21 | AC | 139 ms
64,848 KB |
testcase_22 | AC | 112 ms
50,600 KB |
testcase_23 | AC | 136 ms
55,208 KB |
testcase_24 | AC | 64 ms
38,244 KB |
testcase_25 | AC | 109 ms
50,968 KB |
testcase_26 | AC | 89 ms
46,504 KB |
testcase_27 | AC | 100 ms
46,732 KB |
testcase_28 | AC | 97 ms
46,140 KB |
testcase_29 | AC | 149 ms
64,692 KB |
testcase_30 | AC | 49 ms
37,132 KB |
testcase_31 | AC | 50 ms
37,036 KB |
testcase_32 | AC | 49 ms
37,172 KB |
testcase_33 | AC | 49 ms
36,920 KB |
testcase_34 | AC | 53 ms
37,248 KB |
testcase_35 | AC | 108 ms
46,564 KB |
testcase_36 | AC | 48 ms
37,132 KB |
testcase_37 | AC | 86 ms
42,160 KB |
testcase_38 | AC | 99 ms
46,644 KB |
testcase_39 | AC | 87 ms
46,456 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static int[] weights; static int[] values; static int[][] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); weights = new int[n]; values = new int[n]; int totalWeight = 0; int totalValue = 0; for (int i = 0; i < n; i++) { values[i] = sc.nextInt(); totalValue += values[i]; weights[i] = sc.nextInt(); totalWeight += weights[i]; } int v = sc.nextInt(); dp = new int[n][totalWeight + 1]; for (int[] arr : dp) { Arrays.fill(arr, -1); } int left1 = 0; int right1 = totalWeight; while (right1 - left1 > 1) { int m = (left1 + right1) / 2; if (dfw(n - 1, m) >= v) { right1 = m; } else { left1 = m; } } System.out.println(right1); if (v == totalValue) { System.out.println("inf"); return; } int left2 = 0; int right2 = totalWeight; while (right2 - left2 > 1) { int m = (left2 + right2) / 2; if (dfw(n - 1, m) > v) { right2 = m; } else { left2 = m; } } System.out.println(left2); } static int dfw(int idx, int v) { if (v < 0) { return Integer.MIN_VALUE; } if (idx < 0) { return 0; } if (dp[idx][v] < 0) { dp[idx][v] = Math.max(dfw(idx - 1, v), dfw(idx - 1, v - weights[idx]) + values[idx]); } return dp[idx][v]; } } 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(); } }