結果

問題 No.527 ナップサック容量問題
ユーザー RubikunRubikun
提出日時 2019-08-05 14:30:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,703 bytes
コンパイル時間 1,611 ms
コンパイル使用メモリ 168,776 KB
実行使用メモリ 43,104 KB
最終ジャッジ日時 2023-09-25 17:15:40
合計ジャッジ時間 4,802 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 3 ms
4,696 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 22 ms
19,476 KB
testcase_06 AC 43 ms
36,988 KB
testcase_07 WA -
testcase_08 AC 18 ms
17,320 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 WA -
testcase_11 AC 34 ms
27,232 KB
testcase_12 AC 22 ms
20,712 KB
testcase_13 AC 42 ms
38,116 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 5 ms
7,360 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

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++){
                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++){
                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