結果

問題 No.527 ナップサック容量問題
ユーザー tentententen
提出日時 2021-11-02 11:50:16
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,511 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 79,100 KB
実行使用メモリ 325,008 KB
最終ジャッジ日時 2024-04-19 18:10:13
合計ジャッジ時間 24,420 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
36,972 KB
testcase_01 AC 48 ms
36,788 KB
testcase_02 AC 49 ms
36,776 KB
testcase_03 AC 49 ms
36,976 KB
testcase_04 AC 48 ms
36,956 KB
testcase_05 AC 400 ms
100,312 KB
testcase_06 TLE -
testcase_07 AC 1,005 ms
154,932 KB
testcase_08 AC 342 ms
76,356 KB
testcase_09 AC 53 ms
36,880 KB
testcase_10 AC 1,918 ms
306,492 KB
testcase_11 AC 843 ms
154,224 KB
testcase_12 AC 467 ms
102,884 KB
testcase_13 AC 1,920 ms
325,008 KB
testcase_14 AC 291 ms
69,788 KB
testcase_15 AC 582 ms
109,720 KB
testcase_16 AC 51 ms
36,712 KB
testcase_17 AC 387 ms
89,412 KB
testcase_18 AC 604 ms
108,768 KB
testcase_19 AC 49 ms
37,028 KB
testcase_20 AC 191 ms
58,264 KB
testcase_21 AC 1,389 ms
219,976 KB
testcase_22 AC 812 ms
129,172 KB
testcase_23 AC 1,287 ms
183,784 KB
testcase_24 AC 93 ms
45,464 KB
testcase_25 AC 689 ms
124,384 KB
testcase_26 AC 630 ms
110,100 KB
testcase_27 AC 633 ms
110,184 KB
testcase_28 AC 516 ms
93,132 KB
testcase_29 AC 1,434 ms
254,268 KB
testcase_30 AC 50 ms
37,164 KB
testcase_31 AC 59 ms
37,056 KB
testcase_32 AC 74 ms
37,688 KB
testcase_33 AC 62 ms
38,064 KB
testcase_34 AC 76 ms
38,636 KB
testcase_35 AC 613 ms
109,384 KB
testcase_36 AC 50 ms
36,940 KB
testcase_37 AC 273 ms
70,488 KB
testcase_38 AC 473 ms
90,880 KB
testcase_39 AC 487 ms
92,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static int[] weights;
    static int[] values;
    static ArrayList<HashMap<Integer, Integer>> dp = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int totalW = 0;;
        int totalV = 0;
        weights = new int[n];
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            totalV += values[i];
            weights[i] = sc.nextInt();
            totalW += weights[i];
            dp.add(new HashMap<>());
        }
        int v = sc.nextInt();
        int left1 = 0;
        int right1 = totalW;
        while (right1 - left1 > 1) {
            int m = (left1 + right1) / 2;
            if (dfw(n - 1, m) >= v) {
                right1 = m;
            } else {
                left1 = m;
            }
        }
        if (totalV == v) {
            System.out.println(right1);
            System.out.println("inf");
            return;
        }
        int left2 = right1;
        int right2 = totalW;
        while (right2 - left2 > 1) {
            int m = (left2 + right2) / 2;
            if (dfw(n - 1, m) > v) {
                right2 = m;
            } else {
                left2 = m;
            }
        }
        System.out.println(right1);
        System.out.println(left2);
    }
    
    static int dfw(int idx, int w) {
        if (w < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp.get(idx).containsKey(w)) {
            return dp.get(idx).get(w);
        }
        int ans = Math.max(dfw(idx - 1, w), dfw(idx - 1, w - weights[idx]) + values[idx]);
        dp.get(idx).put(w, ans);
        return ans;
    }
}
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 nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0