結果

問題 No.527 ナップサック容量問題
ユーザー RubikunRubikun
提出日時 2019-08-05 14:39:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,861 bytes
コンパイル時間 1,488 ms
コンパイル使用メモリ 168,852 KB
実行使用メモリ 42,824 KB
最終ジャッジ日時 2023-09-25 17:16:10
合計ジャッジ時間 4,055 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,976 KB
testcase_01 AC 4 ms
5,276 KB
testcase_02 AC 2 ms
4,544 KB
testcase_03 AC 4 ms
5,320 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 23 ms
19,420 KB
testcase_06 AC 46 ms
36,920 KB
testcase_07 AC 42 ms
30,276 KB
testcase_08 AC 18 ms
16,988 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 46 ms
37,356 KB
testcase_11 AC 35 ms
27,152 KB
testcase_12 AC 24 ms
20,656 KB
testcase_13 AC 46 ms
38,160 KB
testcase_14 AC 13 ms
15,872 KB
testcase_15 AC 42 ms
23,244 KB
testcase_16 AC 4 ms
5,272 KB
testcase_17 AC 23 ms
19,816 KB
testcase_18 AC 27 ms
22,076 KB
testcase_19 AC 4 ms
5,696 KB
testcase_20 AC 19 ms
16,800 KB
testcase_21 AC 55 ms
42,824 KB
testcase_22 AC 41 ms
29,636 KB
testcase_23 AC 50 ms
41,596 KB
testcase_24 AC 8 ms
11,180 KB
testcase_25 AC 36 ms
28,812 KB
testcase_26 AC 35 ms
26,052 KB
testcase_27 AC 35 ms
26,452 KB
testcase_28 AC 42 ms
23,284 KB
testcase_29 AC 54 ms
42,388 KB
testcase_30 AC 5 ms
7,224 KB
testcase_31 AC 25 ms
20,260 KB
testcase_32 AC 36 ms
24,096 KB
testcase_33 AC 27 ms
20,988 KB
testcase_34 AC 34 ms
25,084 KB
testcase_35 AC 34 ms
24,856 KB
testcase_36 AC 3 ms
4,964 KB
testcase_37 AC 24 ms
19,336 KB
testcase_38 AC 37 ms
23,776 KB
testcase_39 AC 28 ms
22,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100003,INF=1<<30;

int main(){

    int N;cin>>N;
    vector<int> V(N),W(N);
    int sum=0;
    for(int i=0;i<N;i++){
        cin>>V[i]>>W[i];
        sum+=V[i];
    }
    int maxi;cin>>maxi;
    
    if(sum==maxi){
        int dp[N+1][MAX];//i個みたときの合計容量jのときの価値max
        for(int i=0;i<MAX;i++){
            for(int j=0;j<N+1;j++){
                dp[j][i]=0;
            }
        }
        
        for(int i=1;i<=N;i++){
            for(int j=0;j<MAX;j++){
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
                if(j-W[i-1]<0) continue;
                dp[i][j]=max(dp[i][j],dp[i-1][j-W[i-1]]+V[i-1]);
            }
        }
        
        int left=-1,right=-1;
        for(int i=0;i<MAX;i++){
            if(left==-1&&dp[N][i]==maxi){
                left=i;
                break;
            }
        }
        if(left==0) left++;
        
        cout<<left<<endl<<"inf"<<endl;
    }else{
        int dp[N+1][MAX];//i個みたときの合計容量jのときの価値max
        for(int i=0;i<MAX;i++){
            for(int j=0;j<N+1;j++){
                dp[j][i]=0;
            }
        }
        
        for(int i=1;i<=N;i++){
            for(int j=0;j<MAX;j++){
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
                if(j-W[i-1]<0) continue;
                dp[i][j]=max(dp[i][j],dp[i-1][j-W[i-1]]+V[i-1]);
            }
        }
        
        int left=-1,right=-1;
        for(int i=0;i<MAX;i++){
            if(left==-1&&dp[N][i]==maxi) left=i;
            else if(left!=-1&&dp[N][i]>maxi){
                right=i-1;
                break;
            }
        }
        if(left==0) left++;
        
        cout<<left<<endl<<right<<endl;
    }
}
0