結果

問題 No.527 ナップサック容量問題
ユーザー peroonperoon
提出日時 2019-04-25 16:20:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,724 bytes
コンパイル時間 1,759 ms
コンパイル使用メモリ 173,196 KB
実行使用メモリ 81,536 KB
最終ジャッジ日時 2024-04-30 02:21:42
合計ジャッジ時間 6,034 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
6,528 KB
testcase_01 AC 9 ms
7,296 KB
testcase_02 AC 9 ms
6,528 KB
testcase_03 AC 9 ms
7,424 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 65 ms
36,092 KB
testcase_06 AC 133 ms
70,856 KB
testcase_07 AC 105 ms
57,344 KB
testcase_08 AC 57 ms
31,332 KB
testcase_09 AC 7 ms
5,736 KB
testcase_10 AC 134 ms
71,296 KB
testcase_11 AC 94 ms
51,680 KB
testcase_12 AC 69 ms
38,512 KB
testcase_13 AC 141 ms
73,276 KB
testcase_14 AC 49 ms
28,416 KB
testcase_15 AC 78 ms
43,264 KB
testcase_16 AC 9 ms
7,296 KB
testcase_17 AC 63 ms
36,352 KB
testcase_18 AC 73 ms
40,832 KB
testcase_19 AC 11 ms
8,064 KB
testcase_20 AC 52 ms
29,952 KB
testcase_21 RE -
testcase_22 AC 104 ms
55,936 KB
testcase_23 AC 150 ms
80,128 KB
testcase_24 AC 31 ms
18,944 KB
testcase_25 AC 97 ms
54,144 KB
testcase_26 AC 86 ms
48,640 KB
testcase_27 AC 88 ms
49,408 KB
testcase_28 AC 76 ms
43,264 KB
testcase_29 AC 153 ms
81,536 KB
testcase_30 AC 19 ms
11,916 KB
testcase_31 AC 64 ms
36,992 KB
testcase_32 AC 78 ms
44,800 KB
testcase_33 AC 66 ms
38,528 KB
testcase_34 AC 81 ms
46,336 KB
testcase_35 AC 83 ms
46,336 KB
testcase_36 AC 8 ms
6,784 KB
testcase_37 AC 61 ms
35,584 KB
testcase_38 AC 79 ms
44,288 KB
testcase_39 AC 71 ms
40,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}

ll N;
vector<ll> V;
vector<ll> W;

const int W_MAX = 100010;
// const int W_MAX = 50;
ll memo[100][W_MAX];

// i : i番目を見ている
// j : 残り容量
ll rec(ll i, ll j){
    // cout << "rec " << i << " " << j << endl;
    if(memo[i][j]!=0){
        return memo[i][j];
    }

    ll res;

    if(i==N){
        res = 0;
    }
    // 入れられない
    else if(W[i]>j){
        res = rec(i+1, j);
    }
    else{
        ll ma0 = rec(i+1, j-W[i]) + V[i]; // いれる
        ll ma1 = rec(i+1, j); // いれない
        res = max(ma0, ma1);
    }

    return memo[i][j] = res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    cin >> N;
    V.resize(N);
    W.resize(N);

    FOR(i, 0, N){
        cin >> V[i] >> W[i];
    }

    ll V;
    cin >> V;

    // ll v = rec(0, 100000);
    // pn(v);

    FOR(w, 0, 20){
        // pn(memo[0][w]);
    }

    vector<ll> A;
    FOR(w, 0, W_MAX){
        if(rec(0, w)==V){
            A.push_back(w);
        }
    }
    sort(ALL(A));
    // vprint(A);

    ll mi = A[0];
    ll ma = A.back();

    if(mi==0){
        mi = 1;
    }    
    p(mi);
    
    if(ma>100000){
        p("inf");
    }else{
        p(ma);
    }
    
    return 0;
}
0