結果

問題 No.527 ナップサック容量問題
ユーザー stkz_uq
提出日時 2017-06-10 00:37:44
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,174 bytes
コンパイル時間 456 ms
コンパイル使用メモリ 54,472 KB
実行使用メモリ 13,760 KB
最終ジャッジ日時 2024-09-22 21:15:12
合計ジャッジ時間 4,062 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 2 TLE * 1 -- * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

using namespace std;

int num, item[100][2], v, w;
int W = 0, V = 0;

int func1(int n) {
    if(V == v) {
        return W;
    }
    else if(V > v || n == num) return 2147483647;

    V += item[n][0];
    W += item[n][1];
    int res1 = func1(n+1);
    V -= item[n][0];
    W -= item[n][1];
    int res2 = func1(n+1);

    return (res1 < res2) ? res1 : res2;
}

int func2(int n) {
    if(W > w)   return -1;
    if(n == num) {
        return V;
    }

    V += item[n][0];
    W += item[n][1];
    int res1 = func2(n+1);
    V -= item[n][0];
    W -= item[n][1];
    int res2 = func2(n+1);

    return (res1 > res2) ? res1 : res2;
}

int main() {
    int all = 0;

    cin >> num;
    for (int i=0; i<num; i++) {
        cin >> item[i][0] >> item[i][1];
        all += item[i][1];
    }
    cin >> v;

    int mymin = func1(0);

    w = mymin+1;
    while(func2(0) == v) {
        //cout << func2(0) << " " << w << endl;
        w++;
        
        if(w > all) {
            cout << mymin << endl << "inf" << endl;
            return 0;
        }
    }
    w--;

    if(mymin == 0)  mymin = 1;
    cout << mymin << endl << w << endl;

    return 0;
}
0