結果

問題 No.527 ナップサック容量問題
ユーザー RubikunRubikun
提出日時 2019-08-05 14:38:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,805 bytes
コンパイル時間 1,480 ms
コンパイル使用メモリ 167,644 KB
実行使用メモリ 42,868 KB
最終ジャッジ日時 2023-09-25 17:16:05
合計ジャッジ時間 3,956 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,920 KB
testcase_01 WA -
testcase_02 AC 3 ms
4,800 KB
testcase_03 AC 4 ms
5,324 KB
testcase_04 WA -
testcase_05 AC 25 ms
19,424 KB
testcase_06 AC 52 ms
37,256 KB
testcase_07 AC 40 ms
30,324 KB
testcase_08 AC 23 ms
17,020 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 45 ms
37,452 KB
testcase_11 AC 37 ms
27,192 KB
testcase_12 AC 25 ms
20,544 KB
testcase_13 AC 54 ms
38,120 KB
testcase_14 AC 13 ms
15,856 KB
testcase_15 AC 43 ms
23,292 KB
testcase_16 AC 4 ms
5,312 KB
testcase_17 AC 23 ms
19,776 KB
testcase_18 AC 29 ms
22,108 KB
testcase_19 AC 4 ms
5,768 KB
testcase_20 AC 18 ms
16,768 KB
testcase_21 AC 54 ms
42,868 KB
testcase_22 AC 40 ms
29,648 KB
testcase_23 AC 50 ms
41,808 KB
testcase_24 AC 8 ms
11,216 KB
testcase_25 WA -
testcase_26 AC 36 ms
26,072 KB
testcase_27 AC 35 ms
26,408 KB
testcase_28 WA -
testcase_29 AC 53 ms
42,700 KB
testcase_30 AC 6 ms
7,552 KB
testcase_31 AC 23 ms
20,208 KB
testcase_32 AC 33 ms
24,072 KB
testcase_33 AC 26 ms
21,040 KB
testcase_34 AC 31 ms
25,136 KB
testcase_35 AC 32 ms
24,836 KB
testcase_36 WA -
testcase_37 AC 23 ms
19,372 KB
testcase_38 AC 35 ms
23,740 KB
testcase_39 AC 28 ms
22,104 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;
            }
        }
        
        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;
            }
        }
        
        cout<<left<<endl<<right<<endl;
    }
}
0