結果

問題 No.527 ナップサック容量問題
ユーザー Naoyk1212Naoyk1212
提出日時 2017-06-13 23:59:55
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,341 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 74,612 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 21:54:48
合計ジャッジ時間 1,841 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 6 ms
4,348 KB
testcase_07 WA -
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 WA -
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 6 ms
4,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 2 ms
4,348 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,348 KB
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 2 ms
4,348 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 -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

    vector<int> dp(100010, -1);

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

    int mx = 0;
    int tmp = -1;
    int tmp1 = -1;
    for(int i = 0; i <= 100000; i++){
       if(i <= mx_v)mx = max(mx, dp[i]);

       if(i == mx_v)tmp = dp[i];
       else if(tmp != -1 && tmp < dp[i]){
           tmp1 = dp[i];
           break;
       }
    }

    if(!ok && over)cout << 1 << endl << *min_element(v.begin(), v.end()) << endl;
    else if(ok && !over)cout << mx << endl << "inf" << endl;
    else cout << tmp << endl << tmp1 - 1 << endl;

}
0