結果

問題 No.527 ナップサック容量問題
ユーザー NasatameNasatame
提出日時 2017-06-09 23:02:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,141 bytes
コンパイル時間 649 ms
コンパイル使用メモリ 58,600 KB
実行使用メモリ 398,216 KB
最終ジャッジ日時 2023-10-23 23:49:02
合計ジャッジ時間 6,590 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
19,272 KB
testcase_01 AC 14 ms
23,180 KB
testcase_02 AC 10 ms
15,372 KB
testcase_03 AC 15 ms
23,180 KB
testcase_04 AC 8 ms
11,460 KB
testcase_05 AC 98 ms
163,824 KB
testcase_06 AC 201 ms
339,620 KB
testcase_07 AC 161 ms
273,204 KB
testcase_08 AC 82 ms
140,384 KB
testcase_09 AC 9 ms
11,464 KB
testcase_10 AC 201 ms
343,524 KB
testcase_11 AC 140 ms
241,956 KB
testcase_12 AC 104 ms
175,544 KB
testcase_13 AC 204 ms
351,340 KB
testcase_14 AC 76 ms
128,660 KB
testcase_15 AC 117 ms
202,884 KB
testcase_16 AC 14 ms
23,180 KB
testcase_17 AC 96 ms
167,724 KB
testcase_18 AC 110 ms
191,164 KB
testcase_19 AC 17 ms
27,088 KB
testcase_20 AC 79 ms
136,472 KB
testcase_21 AC 232 ms
398,216 KB
testcase_22 AC 154 ms
265,392 KB
testcase_23 AC 225 ms
386,496 KB
testcase_24 AC 49 ms
81,780 KB
testcase_25 AC 149 ms
257,580 KB
testcase_26 AC 133 ms
230,232 KB
testcase_27 AC 135 ms
234,140 KB
testcase_28 AC 117 ms
202,884 KB
testcase_29 AC 232 ms
394,312 KB
testcase_30 AC 26 ms
42,716 KB
testcase_31 AC 99 ms
171,632 KB
testcase_32 AC 123 ms
210,700 KB
testcase_33 AC 103 ms
179,444 KB
testcase_34 AC 127 ms
218,512 KB
testcase_35 AC 128 ms
218,512 KB
testcase_36 AC 12 ms
19,272 KB
testcase_37 AC 97 ms
163,820 KB
testcase_38 AC 122 ms
206,792 KB
testcase_39 AC 111 ms
191,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
 
 
 /*
N≤100
1≤vi,wi≤10001≤vi,wi≤1000
0≤V≤100000
*/

// 番号 重さ = 価値
int dp[101][1000100] = {};
int main(void){
    
    int n;
    cin >> n;
    
    for(int i = 0; i < n; i++)
        for(int j = 0; j < 1000100; j++)
            dp[i][j] = -1;
    
    
    dp[0][0] = 0;
    
    for(int i = 0; i < n; i++){
        int v,w;
        cin >> v >> w;
        
        for(int j = 1000099; j >= 0; j --){
            if(dp[i][j] != -1 && j + w < 1000099){
                dp[i+1][j+w] = max(v + dp[i][j],dp[i+1][j+w]);
                dp[i+1][j] = max(dp[i][j],dp[i+1][j]);
                //cout << j+w << endl;
            }
        }
    }
    int V,Max = 0,Min = 1000100;
    cin >> V;
    
    for(int j = 0; j < 1000100; j++){
        
        dp[n][j+1] = max(dp[n][j+1],dp[n][j]); 
        
        if(dp[n][j] == V){
            Max = max(Max,j);
            Min = min(Min,j);
        }
    }
    
    cout << max(Min,1) << endl;
    if(Max > 1000000) 
    cout << "inf" << endl;
    else 
    cout << Max << endl; 
    
}
0