結果

問題 No.527 ナップサック容量問題
ユーザー sima.tettekesima.tetteke
提出日時 2019-09-22 17:44:52
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,727 bytes
コンパイル時間 1,597 ms
コンパイル使用メモリ 175,464 KB
実行使用メモリ 10,144 KB
最終ジャッジ日時 2023-10-19 07:20:14
合計ジャッジ時間 8,557 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 90 ms
5,976 KB
testcase_06 AC 389 ms
8,792 KB
testcase_07 WA -
testcase_08 AC 59 ms
5,232 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 WA -
testcase_11 AC 173 ms
6,680 KB
testcase_12 AC 105 ms
6,064 KB
testcase_13 AC 333 ms
8,592 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1 ms
4,348 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,348 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 209 ms
6,836 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 118 ms
6,100 KB
testcase_29 WA -
testcase_30 AC 2 ms
4,348 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 1 ms
4,348 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
ll N;

int main(){
    
    ll N;
    cin >> N;

    vector<pair<ll, ll > > vec;

    ll sum_v = 0;
    ll min_w = 1145141919;
    for(ll i = 0; i < N; i++){
        ll v, w;
        cin >> v >> w;
        vec.push_back(make_pair(v, w));
        sum_v += v;
        if(min_w > w) min_w = w;
    }

    sort(vec.begin(), vec.end());

    ll V;
    cin >> V;

    //DP
    map<ll, ll> dp;
    dp[0] = 0;
    for(ll i = 0; i < N; i++){
        //読み出し
        vector<pair <ll, ll > > tmp;
        auto begin = dp.begin(), end = dp.end();
        for (auto iter = begin; iter != end; iter++) {
            tmp.push_back(make_pair(iter->first, iter->second));
            if(vec[i].first + iter->first <= sum_v){
                tmp.push_back(make_pair(vec[i].first + iter->first, vec[i].second + iter->second));
            }
        }
        //更新 first = 価値 second = 重さ
       //更新
        for (ll j = 0; j < tmp.size(); j++) {
            dp[tmp[j].first] = max(tmp[j].second, dp[tmp[j].first]);
        }  
    }

    ll min_contain = 1;
    ll max_contain = min_w - 1;
    auto begin = dp.begin(), end = dp.end();
    for (auto iter = begin; iter != end; iter++) {
        //cout << iter->first << " " << iter->second << endl;
        if(iter->first == V && V != 0){
            min_contain = iter->second;
        }else if(iter->first > V && V != 0){
            max_contain = iter->second - 1;
            break;
        }
    }

    if(min_contain > max_contain){
        cout << min_contain << endl;
        cout << "inf" << endl;
    }else{
        cout << min_contain << endl;
        cout << max_contain << endl;
    }

    

} 
0