結果

問題 No.527 ナップサック容量問題
ユーザー tentententen
提出日時 2022-10-14 15:31:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 2,034 bytes
コンパイル時間 2,067 ms
コンパイル使用メモリ 74,864 KB
実行使用メモリ 58,492 KB
最終ジャッジ日時 2023-09-08 19:42:53
合計ジャッジ時間 7,534 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,336 KB
testcase_01 AC 42 ms
49,388 KB
testcase_02 AC 42 ms
49,556 KB
testcase_03 AC 42 ms
49,260 KB
testcase_04 AC 42 ms
49,700 KB
testcase_05 AC 87 ms
52,288 KB
testcase_06 AC 159 ms
55,116 KB
testcase_07 AC 139 ms
55,168 KB
testcase_08 AC 87 ms
52,376 KB
testcase_09 AC 42 ms
49,400 KB
testcase_10 AC 184 ms
55,860 KB
testcase_11 AC 110 ms
53,408 KB
testcase_12 AC 99 ms
52,252 KB
testcase_13 AC 196 ms
55,812 KB
testcase_14 AC 72 ms
51,836 KB
testcase_15 AC 109 ms
52,608 KB
testcase_16 AC 44 ms
49,416 KB
testcase_17 AC 88 ms
52,416 KB
testcase_18 AC 100 ms
52,720 KB
testcase_19 AC 44 ms
49,280 KB
testcase_20 AC 83 ms
51,668 KB
testcase_21 AC 197 ms
55,940 KB
testcase_22 AC 128 ms
52,860 KB
testcase_23 AC 196 ms
56,228 KB
testcase_24 AC 51 ms
49,668 KB
testcase_25 AC 119 ms
53,484 KB
testcase_26 AC 121 ms
53,432 KB
testcase_27 AC 107 ms
53,148 KB
testcase_28 AC 106 ms
52,364 KB
testcase_29 AC 251 ms
58,492 KB
testcase_30 AC 43 ms
49,500 KB
testcase_31 AC 71 ms
52,156 KB
testcase_32 AC 86 ms
52,140 KB
testcase_33 AC 81 ms
52,296 KB
testcase_34 AC 88 ms
52,532 KB
testcase_35 AC 83 ms
52,224 KB
testcase_36 AC 42 ms
49,380 KB
testcase_37 AC 64 ms
50,940 KB
testcase_38 AC 71 ms
52,240 KB
testcase_39 AC 71 ms
52,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        TreeMap<Integer, Integer> values = new TreeMap<>();
        values.put(0, 0);
        for (int i = 0; i < n; i++) {
            int v = sc.nextInt();
            int w = sc.nextInt();
            Integer key = Integer.MAX_VALUE;
            while ((key = values.lowerKey(key)) != null) {
                Integer next = key + v;
                int wnext = values.get(key) + w;
                if (values.lastKey() < next || values.ceilingEntry(next).getValue() > wnext) {
                    values.put(next, wnext);
                }
                while ((next = values.lowerKey(next)) != null) {
                    if (values.get(next) >= wnext) {
                        values.remove(next);
                    } else {
                        break;
                    }
                }
            }
        }
        values.put(0, 1);
        int v = sc.nextInt();
        System.out.println(values.get(v));
        if (values.lastKey() == v) {
            System.out.println("inf");
        } else {
            System.out.println(values.higherEntry(v).getValue() - 1);
        }
    }
}
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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0