結果

問題 No.527 ナップサック容量問題
ユーザー sima.tetteke
提出日時 2019-09-22 17:44:52
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,727 bytes
コンパイル時間 1,636 ms
コンパイル使用メモリ 175,864 KB
実行使用メモリ 10,064 KB
最終ジャッジ日時 2024-09-19 03:40:05
合計ジャッジ時間 7,505 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 WA * 23
権限があれば一括ダウンロードができます

ソースコード

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