結果

問題 No.527 ナップサック容量問題
ユーザー Naoyk1212Naoyk1212
提出日時 2017-06-09 23:47:52
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,420 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 74,836 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 02:34:36
合計ジャッジ時間 2,565 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 11 ms
4,348 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 12 ms
4,348 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 WA -
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 -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:16:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |         scanf("%d %d", &v[i], &w[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> v(n), w(n);
    for(int i = 0; i < n; i++){
        scanf("%d %d", &v[i], &w[i]);
    }
    int mx_v;
    cin >> mx_v;

    int dp[100010];
    for(int i = 0; i < 100010; i++)dp[i] = -1;

    bool over = false;
    dp[0] = 0;
    int cnt = 0;
    for(int i = 0; i < n; i++){
        for(int j = 100000; j >= 0; j--){
            if(dp[j] >= 0){
                if(dp[j] + v[i] > mx_v) over = true;
                else if(dp[j] + w[i] <= mx_v){
                    dp[j + w[i]] = dp[j] + v[i];
                }
            }
        }
    }

    int tmp = 0;
    int mn = 1e9;
    int mx = -1;
    bool check = false;
    for(int i = 1; i <= mx_v; i++){
        if(dp[i] == -1)dp[i] = tmp;
        else tmp = dp[i];
        if(dp[i] == mx_v){
            check = true;
            mn = min(i, mn);
            mx = max(i, mx);
        }
    }

    if(check) {
        cout << mn << endl;
        cout << mx << endl;
    }else if(!check && over) {
        cout << 1 << endl;
        cout << *min_element(v.begin(), v.end()) << endl;
    }else{
        int sum = 0;
        for(int i = 0; i < n; i++){
            sum += w[i];
        }
        cout << sum << endl;
        cout << "inf" << endl;
    }
}
0