結果

問題 No.527 ナップサック容量問題
ユーザー んんんんんん
提出日時 2023-01-08 16:37:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 960 bytes
コンパイル時間 3,167 ms
コンパイル使用メモリ 202,692 KB
実行使用メモリ 4,420 KB
最終ジャッジ日時 2023-08-22 02:49:35
合計ジャッジ時間 6,461 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 4 ms
4,376 KB
testcase_02 AC 13 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 155 ms
4,376 KB
testcase_06 AC 382 ms
4,380 KB
testcase_07 AC 54 ms
4,380 KB
testcase_08 AC 130 ms
4,380 KB
testcase_09 AC 10 ms
4,384 KB
testcase_10 AC 169 ms
4,376 KB
testcase_11 AC 243 ms
4,376 KB
testcase_12 AC 167 ms
4,420 KB
testcase_13 AC 398 ms
4,380 KB
testcase_14 AC 27 ms
4,380 KB
testcase_15 AC 32 ms
4,380 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 48 ms
4,380 KB
testcase_18 AC 61 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 15 ms
4,380 KB
testcase_21 AC 41 ms
4,380 KB
testcase_22 AC 28 ms
4,376 KB
testcase_23 AC 38 ms
4,376 KB
testcase_24 AC 10 ms
4,376 KB
testcase_25 AC 26 ms
4,380 KB
testcase_26 AC 23 ms
4,380 KB
testcase_27 AC 24 ms
4,376 KB
testcase_28 AC 21 ms
4,376 KB
testcase_29 AC 39 ms
4,376 KB
testcase_30 AC 36 ms
4,380 KB
testcase_31 AC 19 ms
4,380 KB
testcase_32 AC 22 ms
4,376 KB
testcase_33 AC 19 ms
4,376 KB
testcase_34 AC 23 ms
4,376 KB
testcase_35 AC 67 ms
4,380 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 22 ms
4,376 KB
testcase_38 AC 40 ms
4,376 KB
testcase_39 AC 60 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int N, V;
vector<int> v, w;
vector<int> dp;

int ns(int W) {
    dp.assign(W+1, -1);
    dp[0] = 0;
    for (int i = 0; i < N; i++) {
        for (int j = W; j >= 0; j--) {
            if (j - w[i] >= 0) {
                dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
            }
        }
    }
    int ret = -1;
    for (int i = 0; i <= W; i++) ret = max(ret, dp[i]);
    return ret;
}

int main() {
    cin >> N;
    v.resize(N); w.resize(N);
    for (int i = 0; i < N; i++) cin >> v[i] >> w[i];
    cin >> V;

    int ok = 100000, ng = 0;
    while (ok - ng > 1) {
        int mid = (ok + ng) / 2;
        if (ns(mid) < V) ng = mid;
        else ok = mid;
    }
    cout << ok << endl;
    
    ok = 1, ng = 100002;
    while (ng - ok > 1) {
        int mid = (ok + ng) / 2;
        if (ns(mid) > V) ng = mid;
        else ok = mid;
    }
    if (ok == 100001) cout << "inf" << endl;
    else cout << ok << endl;
}
0