結果

問題 No.527 ナップサック容量問題
ユーザー ytftytft
提出日時 2021-03-19 15:20:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,224 bytes
コンパイル時間 2,363 ms
コンパイル使用メモリ 170,084 KB
実行使用メモリ 42,608 KB
最終ジャッジ日時 2023-08-11 16:21:29
合計ジャッジ時間 5,485 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,844 KB
testcase_01 WA -
testcase_02 AC 3 ms
4,448 KB
testcase_03 AC 5 ms
4,988 KB
testcase_04 WA -
testcase_05 AC 27 ms
19,072 KB
testcase_06 AC 55 ms
36,756 KB
testcase_07 AC 45 ms
30,148 KB
testcase_08 AC 25 ms
16,748 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 56 ms
37,100 KB
testcase_11 AC 39 ms
27,148 KB
testcase_12 AC 29 ms
20,204 KB
testcase_13 AC 59 ms
38,192 KB
testcase_14 AC 22 ms
15,480 KB
testcase_15 AC 35 ms
23,328 KB
testcase_16 AC 5 ms
4,956 KB
testcase_17 AC 29 ms
19,404 KB
testcase_18 AC 32 ms
21,760 KB
testcase_19 AC 6 ms
5,504 KB
testcase_20 AC 23 ms
16,316 KB
testcase_21 AC 64 ms
42,608 KB
testcase_22 AC 42 ms
29,332 KB
testcase_23 AC 62 ms
41,472 KB
testcase_24 AC 14 ms
10,984 KB
testcase_25 WA -
testcase_26 AC 38 ms
25,800 KB
testcase_27 AC 40 ms
26,236 KB
testcase_28 WA -
testcase_29 AC 63 ms
42,120 KB
testcase_30 AC 8 ms
6,964 KB
testcase_31 AC 28 ms
19,872 KB
testcase_32 AC 36 ms
23,864 KB
testcase_33 AC 29 ms
20,656 KB
testcase_34 AC 35 ms
24,660 KB
testcase_35 AC 36 ms
24,752 KB
testcase_36 WA -
testcase_37 AC 27 ms
19,072 KB
testcase_38 AC 35 ms
23,380 KB
testcase_39 AC 31 ms
21,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<int> typical_knapsack(vector<int> weight,vector<int> value,int capacity){
    int N=weight.size();
    vector<vector<int>> dp(N,vector<int>(capacity+1));
    for(int i=0;i<N;i++){
        for(int j=0;j<=capacity+1;j++){
            if(i==0){
                if(j>=weight[0]){
                    dp[i][j]=value[0];
                }else{
                    dp[i][j]=0;
                }
            }else{
                if(j>=weight[i]){
                    dp[i][j]=max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i]);
                }else{
                    dp[i][j]=dp[i-1][j];
                }
            }
        }
    }
    return dp[N-1];
}


int main(){
    int N;
    cin>>N;
    vector<int> capacity(N),value(N);
    for(int i=0;i<N;i++){
        cin>>value[i]>>capacity[i];
    }
    int V;
    cin>>V;
    vector<int> ans=typical_knapsack(capacity,value,100*1000);
    int m=-1;
    int M=-1;
    for(int i=0;i<ans.size();i++){
        if(ans[i]==V && m==-1){
            m=i;
        }
        if(ans[i]>V && M==-1){
            M=i-1;
        }
    }
    cout<<m<<endl;
    if(M==-1){
        cout<<"inf"<<endl;
    }else{
        cout<<M<<endl;
    }
}
0