結果

問題 No.527 ナップサック容量問題
ユーザー tentententen
提出日時 2023-03-07 15:41:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 3,037 bytes
コンパイル時間 2,994 ms
コンパイル使用メモリ 92,224 KB
実行使用メモリ 79,548 KB
最終ジャッジ日時 2023-10-18 05:30:46
合計ジャッジ時間 8,306 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
53,468 KB
testcase_01 AC 56 ms
53,468 KB
testcase_02 AC 55 ms
53,464 KB
testcase_03 AC 56 ms
53,468 KB
testcase_04 AC 55 ms
53,456 KB
testcase_05 AC 94 ms
57,240 KB
testcase_06 AC 174 ms
67,468 KB
testcase_07 AC 123 ms
64,352 KB
testcase_08 AC 82 ms
56,696 KB
testcase_09 AC 54 ms
53,460 KB
testcase_10 AC 157 ms
67,516 KB
testcase_11 AC 114 ms
59,416 KB
testcase_12 AC 95 ms
57,212 KB
testcase_13 AC 177 ms
68,636 KB
testcase_14 AC 79 ms
56,996 KB
testcase_15 AC 98 ms
59,404 KB
testcase_16 AC 55 ms
51,424 KB
testcase_17 AC 94 ms
56,992 KB
testcase_18 AC 101 ms
59,912 KB
testcase_19 AC 55 ms
52,344 KB
testcase_20 AC 85 ms
56,772 KB
testcase_21 AC 154 ms
79,548 KB
testcase_22 AC 125 ms
64,168 KB
testcase_23 AC 147 ms
68,116 KB
testcase_24 AC 64 ms
53,692 KB
testcase_25 AC 116 ms
64,656 KB
testcase_26 AC 102 ms
59,380 KB
testcase_27 AC 101 ms
60,060 KB
testcase_28 AC 101 ms
59,404 KB
testcase_29 AC 152 ms
78,044 KB
testcase_30 AC 55 ms
53,408 KB
testcase_31 AC 55 ms
53,468 KB
testcase_32 AC 60 ms
53,476 KB
testcase_33 AC 56 ms
53,472 KB
testcase_34 AC 57 ms
53,472 KB
testcase_35 AC 103 ms
59,424 KB
testcase_36 AC 54 ms
53,468 KB
testcase_37 AC 90 ms
56,976 KB
testcase_38 AC 104 ms
59,756 KB
testcase_39 AC 97 ms
59,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0