結果

問題 No.527 ナップサック容量問題
ユーザー ytftytft
提出日時 2021-03-19 15:20:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,224 bytes
コンパイル時間 1,653 ms
コンパイル使用メモリ 171,856 KB
実行使用メモリ 42,860 KB
最終ジャッジ日時 2024-04-29 08:13:07
合計ジャッジ時間 3,947 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 25 ms
19,340 KB
testcase_06 AC 50 ms
36,980 KB
testcase_07 AC 40 ms
30,444 KB
testcase_08 AC 22 ms
16,988 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 50 ms
37,376 KB
testcase_11 AC 37 ms
27,180 KB
testcase_12 AC 27 ms
20,520 KB
testcase_13 AC 51 ms
38,156 KB
testcase_14 AC 20 ms
15,944 KB
testcase_15 AC 30 ms
23,260 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 24 ms
19,736 KB
testcase_18 AC 29 ms
22,080 KB
testcase_19 AC 5 ms
5,616 KB
testcase_20 AC 21 ms
16,596 KB
testcase_21 AC 60 ms
42,860 KB
testcase_22 AC 40 ms
29,532 KB
testcase_23 AC 57 ms
41,812 KB
testcase_24 AC 13 ms
11,108 KB
testcase_25 WA -
testcase_26 AC 34 ms
26,004 KB
testcase_27 AC 36 ms
26,396 KB
testcase_28 WA -
testcase_29 AC 58 ms
42,452 KB
testcase_30 AC 7 ms
7,188 KB
testcase_31 AC 26 ms
20,120 KB
testcase_32 AC 31 ms
23,916 KB
testcase_33 AC 27 ms
20,912 KB
testcase_34 AC 33 ms
24,832 KB
testcase_35 AC 33 ms
24,832 KB
testcase_36 WA -
testcase_37 AC 25 ms
19,336 KB
testcase_38 AC 30 ms
23,776 KB
testcase_39 AC 28 ms
22,084 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