結果

問題 No.527 ナップサック容量問題
ユーザー HachimoriHachimori
提出日時 2017-06-09 23:12:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,100 bytes
コンパイル時間 1,867 ms
コンパイル使用メモリ 58,860 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-24 01:10:41
合計ジャッジ時間 3,043 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 5 ms
4,372 KB
testcase_06 AC 10 ms
4,372 KB
testcase_07 AC 8 ms
4,372 KB
testcase_08 AC 5 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 9 ms
4,372 KB
testcase_11 AC 7 ms
4,372 KB
testcase_12 AC 6 ms
4,372 KB
testcase_13 AC 9 ms
4,372 KB
testcase_14 AC 5 ms
4,372 KB
testcase_15 AC 7 ms
4,372 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 5 ms
4,372 KB
testcase_18 AC 6 ms
4,372 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 4 ms
4,372 KB
testcase_21 AC 10 ms
4,372 KB
testcase_22 AC 7 ms
4,372 KB
testcase_23 AC 10 ms
4,372 KB
testcase_24 AC 3 ms
4,372 KB
testcase_25 AC 7 ms
4,372 KB
testcase_26 AC 6 ms
4,372 KB
testcase_27 AC 7 ms
4,372 KB
testcase_28 AC 6 ms
4,372 KB
testcase_29 AC 11 ms
4,372 KB
testcase_30 AC 3 ms
4,372 KB
testcase_31 AC 5 ms
4,372 KB
testcase_32 AC 6 ms
4,372 KB
testcase_33 AC 5 ms
4,372 KB
testcase_34 AC 6 ms
4,372 KB
testcase_35 AC 6 ms
4,372 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 6 ms
4,372 KB
testcase_38 AC 6 ms
4,372 KB
testcase_39 AC 5 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<numeric>
#include<cstring>
using namespace std;
const int N_BURDEN = 105;
const int SUM_VALUE = 100005;


int N;
int value[N_BURDEN], weight[N_BURDEN];
int V;

void read() {
    cin >> N;
    for (int i = 0; i < N; ++i) {
        cin >> value[i] >> weight[i];
    }
    cin >> V;
}


void work() {
    int maxV[SUM_VALUE];
    memset(maxV, -1, sizeof(maxV));
    maxV[0] = 0;

    for (int i = 0; i < N; ++i) {
        for (int j = SUM_VALUE - 1; j >= 0; --j) {
            if (maxV[j] != -1) {
                maxV[j + weight[i]] = max(maxV[j + weight[i]], maxV[j] + value[i]);
            }
        }
    }
    
    int idx;
    for (idx = 0;; ++idx) {
        if (maxV[idx] == V) {
            if (idx == 0) ++idx;
            cout << idx << endl;
            break;
        }
    }
    
    if (maxV[accumulate(weight, weight + N, 0)] == V) {
        cout << "inf" << endl;
    } else {
        while (maxV[idx] == -1 || maxV[idx] <= V) ++idx;
        cout << max(1, idx - 1) << endl;
    }
}


int main() {
    read();
    work();
    return 0;
}
0