結果

問題 No.527 ナップサック容量問題
ユーザー kagasankagasan
提出日時 2019-09-22 22:16:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,149 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 169,292 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 07:25:46
合計ジャッジ時間 3,371 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll (i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for(ll (i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for(ll (i) = (n) - 1; (i) >= 0; (i)--)
#define rrep1(i, n) for(ll (i) = (n); (i) >= 1; (i)--)
const ll INF = 1145141919;
const ll MOD = 1000000007;
template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}

int main(){

    ll N;
    cin >> N;
    vector<ll>dp(101010, 0);
    rep(i, N){
        ll v, w;
        cin >> v >> w;
        rrep1(to, 100000){
            ll from = to - w;
            if(from < 0)break;
            chmax(dp[to], dp[from] + v);
        }
    }
    ll V;
    cin >> V;
    ll mn = 0, mx = 0;
    for(ll i = 0; i <= 100000; i++){
        if(dp[i] == V && mn == 0){
            mn = 1;
            if(i == 0)cout << 1 << endl;
            else cout << i << endl;
        }
        if(dp[i] > V && mx == 0){
            mx = 1;
            cout << i - 1 << endl;
        }
    }
    if(mx == 0)cout << "inf" << endl;

    return 0;
}
0