結果

問題 No.527 ナップサック容量問題
ユーザー ytftytft
提出日時 2021-03-19 15:22:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,238 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 170,396 KB
実行使用メモリ 42,560 KB
最終ジャッジ日時 2023-08-11 16:23:06
合計ジャッジ時間 4,670 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,616 KB
testcase_01 AC 4 ms
5,000 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 AC 4 ms
5,076 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 27 ms
19,240 KB
testcase_06 AC 53 ms
36,824 KB
testcase_07 AC 44 ms
30,032 KB
testcase_08 AC 23 ms
16,868 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 53 ms
37,092 KB
testcase_11 AC 39 ms
27,112 KB
testcase_12 AC 28 ms
20,384 KB
testcase_13 AC 56 ms
37,956 KB
testcase_14 AC 21 ms
15,528 KB
testcase_15 AC 33 ms
23,164 KB
testcase_16 AC 5 ms
4,896 KB
testcase_17 AC 27 ms
19,444 KB
testcase_18 AC 32 ms
21,816 KB
testcase_19 AC 5 ms
5,512 KB
testcase_20 AC 22 ms
16,396 KB
testcase_21 AC 62 ms
42,560 KB
testcase_22 AC 41 ms
29,248 KB
testcase_23 AC 61 ms
41,400 KB
testcase_24 AC 14 ms
10,884 KB
testcase_25 AC 41 ms
28,512 KB
testcase_26 AC 37 ms
25,844 KB
testcase_27 AC 37 ms
26,232 KB
testcase_28 AC 33 ms
23,120 KB
testcase_29 AC 62 ms
42,264 KB
testcase_30 AC 8 ms
6,924 KB
testcase_31 AC 28 ms
20,008 KB
testcase_32 AC 34 ms
24,144 KB
testcase_33 AC 29 ms
20,808 KB
testcase_34 AC 36 ms
24,644 KB
testcase_35 AC 36 ms
24,704 KB
testcase_36 AC 4 ms
4,620 KB
testcase_37 AC 27 ms
19,168 KB
testcase_38 AC 33 ms
23,320 KB
testcase_39 AC 31 ms
21,752 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<<max(1,m)<<endl;
    if(M==-1){
        cout<<"inf"<<endl;
    }else{
        cout<<max(1,M)<<endl;
    }
}
0