結果

問題 No.527 ナップサック容量問題
ユーザー tentententen
提出日時 2021-11-02 11:57:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 749 ms / 2,000 ms
コード長 3,229 bytes
コンパイル時間 2,204 ms
コンパイル使用メモリ 79,812 KB
実行使用メモリ 149,200 KB
最終ジャッジ日時 2024-04-19 18:20:45
合計ジャッジ時間 12,156 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
50,472 KB
testcase_01 AC 50 ms
50,544 KB
testcase_02 AC 49 ms
50,612 KB
testcase_03 AC 50 ms
50,088 KB
testcase_04 AC 49 ms
50,400 KB
testcase_05 AC 113 ms
60,016 KB
testcase_06 AC 471 ms
122,116 KB
testcase_07 AC 367 ms
102,196 KB
testcase_08 AC 103 ms
56,696 KB
testcase_09 AC 51 ms
50,564 KB
testcase_10 AC 528 ms
131,900 KB
testcase_11 AC 192 ms
71,480 KB
testcase_12 AC 131 ms
63,436 KB
testcase_13 AC 476 ms
120,112 KB
testcase_14 AC 123 ms
61,144 KB
testcase_15 AC 185 ms
69,360 KB
testcase_16 AC 51 ms
50,180 KB
testcase_17 AC 130 ms
63,088 KB
testcase_18 AC 173 ms
66,184 KB
testcase_19 AC 49 ms
50,540 KB
testcase_20 AC 108 ms
58,096 KB
testcase_21 AC 674 ms
132,884 KB
testcase_22 AC 330 ms
88,196 KB
testcase_23 AC 468 ms
112,532 KB
testcase_24 AC 86 ms
51,884 KB
testcase_25 AC 315 ms
85,860 KB
testcase_26 AC 213 ms
78,536 KB
testcase_27 AC 308 ms
86,112 KB
testcase_28 AC 176 ms
66,980 KB
testcase_29 AC 749 ms
149,200 KB
testcase_30 AC 50 ms
50,560 KB
testcase_31 AC 51 ms
50,340 KB
testcase_32 AC 54 ms
50,540 KB
testcase_33 AC 54 ms
50,564 KB
testcase_34 AC 57 ms
50,588 KB
testcase_35 AC 206 ms
75,604 KB
testcase_36 AC 50 ms
50,584 KB
testcase_37 AC 121 ms
60,800 KB
testcase_38 AC 172 ms
66,616 KB
testcase_39 AC 146 ms
64,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static Nap[] naps;
    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;
        naps = new Nap[n];
        for (int i = 0; i < n; i++) {
            naps[i] = new Nap(sc.nextInt(), sc.nextInt());
            totalV += naps[i].value;
            totalW += naps[i].weight;
            dp.add(new HashMap<>());
        }
        Arrays.sort(naps);
        weights = new int[n];
        values = new int[n];
        weights[0] = naps[0].weight;
        values[0] = naps[0].value;
        for (int i = 1; i < n; i++) {
            weights[i] = weights[i - 1] + naps[i].weight;
            values[i] = values[i - 1] + naps[i].value;
        }
        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);
        }
        if (w >= weights[idx]) {
            return values[idx];
        }
        int ans = Math.max(dfw(idx - 1, w), dfw(idx - 1, w - naps[idx].weight) + naps[idx].value);
        dp.get(idx).put(w, ans);
        return ans;
    }
    
    static class Nap implements Comparable<Nap> {
        int value;
        int weight;
        
        public Nap(int value, int weight) {
            this.value = value;
            this.weight = weight;
        }
        
        public int compareTo(Nap another) {
            return weight - another.weight;
        }
    }
}
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