結果

問題 No.527 ナップサック容量問題
ユーザー ama_nukoama_nuko
提出日時 2018-06-12 02:31:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 1,187 bytes
コンパイル時間 784 ms
コンパイル使用メモリ 95,948 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-30 13:49:43
合計ジャッジ時間 2,292 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 7 ms
5,376 KB
testcase_06 AC 12 ms
5,376 KB
testcase_07 AC 10 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 12 ms
5,376 KB
testcase_11 AC 9 ms
5,376 KB
testcase_12 AC 7 ms
5,376 KB
testcase_13 AC 12 ms
5,376 KB
testcase_14 AC 6 ms
5,376 KB
testcase_15 AC 8 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 8 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 13 ms
5,376 KB
testcase_22 AC 10 ms
5,376 KB
testcase_23 AC 13 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 AC 9 ms
5,376 KB
testcase_27 AC 8 ms
5,376 KB
testcase_28 AC 7 ms
5,376 KB
testcase_29 AC 12 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 7 ms
5,376 KB
testcase_32 AC 8 ms
5,376 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 9 ms
5,376 KB
testcase_35 AC 8 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 7 ms
5,376 KB
testcase_38 AC 8 ms
5,376 KB
testcase_39 AC 8 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/iostream:39,
                 from main.cpp:2:
In member function 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>]',
    inlined from 'int main()' at main.cpp:67:17:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:202:25: warning: 'max' may be used uninitialized [-Wmaybe-uninitialized]
  202 |       { return _M_insert(__n); }
      |                ~~~~~~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:51:9: note: 'max' was declared here
   51 |     int max;
      |         ^~~
In member function 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>]',
    inlined from 'int main()' at main.cpp:62:17:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:202:25: warning: 'min' may be used uninitialized [-Wmaybe-uninitialized]
  202 |       { return _M_insert(__n); }
      |                ~~~~~~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:43:9: note: 'min' was declared here
   43 |     int min;
      |         ^~~

ソースコード

diff #

#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

signed main(){
    int n, vv;
    cin >> n;
    vector<int> v(n), w(n);
    rep(i, n) cin >> v[i] >> w[i];
    cin >> vv;

    vector<int> dp(100001);
    rep(i, n){
        for(int j = 100000; j >= 0; j--){
            if(j < w[i]){
                continue;
            }
            dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
        }
    }

    int sum = 0;
    rep(i, n){
        sum += v[i];
    }

    int min;
    rep(i, 100001){
        if(dp[i] == vv){
            min = i;
            break;
        }
    }

    int max;
    for(int i = 100000; i >= 0; i--){
        if(dp[i] == vv){
            max = i;
            break;
        }
    }

    if(min == 0){
        cout << 1 << endl;
    }else{
        cout << min << endl;
    }
    if(sum == vv){
        cout << "inf" << endl;
    }else{
        cout << max << endl;
    }

    return 0;
}
0