結果

問題 No.527 ナップサック容量問題
ユーザー htensaihtensai
提出日時 2019-12-26 19:10:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 2,042 bytes
コンパイル時間 2,315 ms
コンパイル使用メモリ 78,200 KB
実行使用メモリ 76,500 KB
最終ジャッジ日時 2024-04-15 05:51:48
合計ジャッジ時間 10,246 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
54,160 KB
testcase_01 AC 138 ms
53,840 KB
testcase_02 AC 133 ms
54,028 KB
testcase_03 AC 137 ms
54,348 KB
testcase_04 AC 133 ms
53,716 KB
testcase_05 AC 143 ms
54,228 KB
testcase_06 AC 145 ms
53,948 KB
testcase_07 AC 200 ms
63,544 KB
testcase_08 AC 144 ms
53,948 KB
testcase_09 AC 135 ms
53,916 KB
testcase_10 AC 225 ms
66,156 KB
testcase_11 AC 147 ms
54,324 KB
testcase_12 AC 146 ms
54,132 KB
testcase_13 AC 148 ms
54,116 KB
testcase_14 AC 179 ms
56,388 KB
testcase_15 AC 185 ms
58,796 KB
testcase_16 AC 136 ms
53,844 KB
testcase_17 AC 182 ms
58,956 KB
testcase_18 AC 189 ms
59,264 KB
testcase_19 AC 136 ms
54,256 KB
testcase_20 AC 177 ms
56,016 KB
testcase_21 AC 232 ms
76,500 KB
testcase_22 AC 199 ms
63,508 KB
testcase_23 AC 210 ms
67,424 KB
testcase_24 AC 167 ms
54,080 KB
testcase_25 AC 192 ms
63,236 KB
testcase_26 AC 187 ms
59,092 KB
testcase_27 AC 190 ms
63,172 KB
testcase_28 AC 193 ms
59,260 KB
testcase_29 AC 229 ms
76,316 KB
testcase_30 AC 133 ms
53,852 KB
testcase_31 AC 144 ms
54,160 KB
testcase_32 AC 152 ms
54,396 KB
testcase_33 AC 151 ms
54,456 KB
testcase_34 AC 152 ms
54,120 KB
testcase_35 AC 190 ms
59,124 KB
testcase_36 AC 135 ms
54,076 KB
testcase_37 AC 176 ms
59,088 KB
testcase_38 AC 172 ms
59,008 KB
testcase_39 AC 178 ms
59,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.math.*;

public class Main {
    static int[][] dp;
    static int[] wArr;
    static int[] vArr;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int wTotal = 0;
        int vTotal = 0;
        wArr = new int[n + 1];
        vArr = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            vArr[i] = sc.nextInt();
            vTotal += vArr[i];
            wArr[i] = sc.nextInt();
            wTotal += wArr[i];
        }
        int value = sc.nextInt();
        if (value == vTotal) {
            System.out.println(wTotal);
            System.out.println("inf");
            return;
        }
        dp = new int[n + 1][wTotal];
        int maxLeft = 0;
        int maxRight = wTotal;
        while (maxRight - maxLeft > 1) {
            int m = (maxLeft + maxRight) / 2;
            if (dfw(n, m) > value) {
                maxRight = m;
            } else {
                maxLeft = m;
            }
        }
        int max = maxLeft;
        int min;
        if (value == 0) {
            min = 1;
        } else {
            int minLeft = 0;
            int minRight = wTotal;
            while (minRight - minLeft > 1) {
                int m = (minLeft + minRight) / 2;
                if (dfw(n, m) >= value) {
                    minRight = m;
                } else {
                    minLeft = m;
                }
            }
            min = minRight;
        }
        System.out.println(min);
        System.out.println(max);
    }
    
    static int dfw(int idx, int weight) {
        if (weight < 0) {
            return Integer.MIN_VALUE;
        }
        if (weight == 0) {
            return 0;
        }
        if (idx == 0) {
            return 0;
        }
        if (dp[idx][weight] != 0) {
            return dp[idx][weight];
        }
        dp[idx][weight] = Math.max(dfw(idx - 1, weight), dfw(idx - 1, weight - wArr[idx]) + vArr[idx]);
        return dp[idx][weight];
    }
}
0