結果

問題 No.527 ナップサック容量問題
ユーザー HIcoderHIcoder
提出日時 2023-07-23 11:47:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,314 bytes
コンパイル時間 1,072 ms
コンパイル使用メモリ 115,348 KB
実行使用メモリ 83,340 KB
最終ジャッジ日時 2023-10-24 18:07:29
合計ジャッジ時間 3,669 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,308 KB
testcase_01 AC 6 ms
8,100 KB
testcase_02 AC 4 ms
6,516 KB
testcase_03 AC 6 ms
8,100 KB
testcase_04 AC 4 ms
5,724 KB
testcase_05 AC 32 ms
36,348 KB
testcase_06 AC 63 ms
71,460 KB
testcase_07 AC 52 ms
58,260 KB
testcase_08 AC 26 ms
31,596 KB
testcase_09 AC 4 ms
5,724 KB
testcase_10 AC 64 ms
72,252 KB
testcase_11 AC 46 ms
51,924 KB
testcase_12 AC 33 ms
38,724 KB
testcase_13 AC 66 ms
73,836 KB
testcase_14 AC 26 ms
29,220 KB
testcase_15 AC 38 ms
44,004 KB
testcase_16 AC 6 ms
8,100 KB
testcase_17 AC 32 ms
37,140 KB
testcase_18 AC 37 ms
41,892 KB
testcase_19 AC 6 ms
8,892 KB
testcase_20 AC 27 ms
30,804 KB
testcase_21 AC 73 ms
83,340 KB
testcase_22 AC 49 ms
56,676 KB
testcase_23 AC 68 ms
80,964 KB
testcase_24 AC 16 ms
19,716 KB
testcase_25 AC 47 ms
55,092 KB
testcase_26 AC 43 ms
49,548 KB
testcase_27 AC 43 ms
50,340 KB
testcase_28 AC 38 ms
44,004 KB
testcase_29 AC 73 ms
82,548 KB
testcase_30 AC 11 ms
12,060 KB
testcase_31 AC 34 ms
37,932 KB
testcase_32 AC 41 ms
45,588 KB
testcase_33 AC 36 ms
39,516 KB
testcase_34 AC 43 ms
47,172 KB
testcase_35 AC 41 ms
47,172 KB
testcase_36 AC 5 ms
7,308 KB
testcase_37 AC 31 ms
36,348 KB
testcase_38 AC 39 ms
44,796 KB
testcase_39 AC 36 ms
41,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set> 
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
#include <unordered_set>
#include<cassert>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=1e9+7;

int main(){
    int N;
    cin>>N;
    vector<P> vw(N);

    ll sumv=0;
    for(int i=0;i<N;i++){
        cin>>vw[i].first>>vw[i].second;
        sumv+=vw[i].first;
    }
    ll V;
    cin>>V;

    const int MAXW=1e5 + 10;
    /*
    dp[i][j]=i番目までの品を用いて 容量jで作れる価値のmax
    */
    //0にすることがポイント
    vector<vector<ll>> dp(N+1,vector<ll>(MAXW+1,0));
    
    for(int i=0;i<N;i++){
        auto [v,w]=vw[i];
        for(int j=0;j<=MAXW;j++){
            
            dp[i+1][j]=max(dp[i][j],dp[i+1][j]);

            if(j+w<=MAXW){
                dp[i+1][j+w]=max(dp[i+1][j+w],dp[i][j]+v);
            }
        }
    }   

    ll minw=INF;
    ll maxw=0;

    for(ll j=1;j<=MAXW;j++){
        if(dp[N][j]==V){
            minw=min(j,minw);
            maxw=max(j,maxw);
        }
    }

    cout<<minw<<endl;


    if(maxw==MAXW){
        cout<<"inf"<<endl;
    }else{
        cout<<maxw<<endl;
    }


    



}
0