結果

問題 No.527 ナップサック容量問題
ユーザー PachicobuePachicobue
提出日時 2017-06-09 23:45:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,904 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 168,124 KB
実行使用メモリ 42,572 KB
最終ジャッジ日時 2023-10-24 02:29:24
合計ジャッジ時間 3,021 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,704 KB
testcase_01 AC 2 ms
7,704 KB
testcase_02 AC 3 ms
7,708 KB
testcase_03 AC 3 ms
7,720 KB
testcase_04 AC 2 ms
5,656 KB
testcase_05 AC 7 ms
22,208 KB
testcase_06 AC 14 ms
38,740 KB
testcase_07 AC 10 ms
32,420 KB
testcase_08 AC 6 ms
20,132 KB
testcase_09 AC 3 ms
5,660 KB
testcase_10 AC 15 ms
40,796 KB
testcase_11 AC 10 ms
30,452 KB
testcase_12 AC 7 ms
22,216 KB
testcase_13 AC 14 ms
40,764 KB
testcase_14 AC 5 ms
18,040 KB
testcase_15 AC 7 ms
26,256 KB
testcase_16 AC 2 ms
7,712 KB
testcase_17 AC 8 ms
22,188 KB
testcase_18 AC 8 ms
24,276 KB
testcase_19 AC 3 ms
7,716 KB
testcase_20 AC 5 ms
17,972 KB
testcase_21 AC 11 ms
42,572 KB
testcase_22 AC 8 ms
32,312 KB
testcase_23 AC 10 ms
42,552 KB
testcase_24 AC 4 ms
13,876 KB
testcase_25 AC 7 ms
30,232 KB
testcase_26 AC 7 ms
28,196 KB
testcase_27 AC 7 ms
28,188 KB
testcase_28 AC 6 ms
26,136 KB
testcase_29 AC 10 ms
42,532 KB
testcase_30 AC 3 ms
9,780 KB
testcase_31 AC 6 ms
22,160 KB
testcase_32 AC 9 ms
26,356 KB
testcase_33 AC 7 ms
24,256 KB
testcase_34 AC 8 ms
28,308 KB
testcase_35 AC 6 ms
26,136 KB
testcase_36 AC 3 ms
7,704 KB
testcase_37 AC 6 ms
22,040 KB
testcase_38 AC 7 ms
26,136 KB
testcase_39 AC 6 ms
24,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define pii pair<int, int>
#define vi vector<int>

using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p)
{
    return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc)
{
    o << "sz = " << vc.size() << endl
      << "[";
    for (const T& v : vc)
        o << v << ",";
    o << "]";
    return o;
}
using ll = long long;
constexpr ll MOD = 1000000007;
constexpr int MAX = 100;
constexpr int WMAX = 100000;
constexpr int VMAX = 100000;

constexpr int INF = numeric_limits<int>::max() / 10;

pii p[MAX];
int dp[MAX + 1][VMAX + 2];  // min weight i~ nolessthan v

int main()
{
    int N;
    cin >> N;
    rep(i, N)
    {
        cin >> p[i].first >> p[i].second;
    }
    int V;
    cin >> V;
    for (int v = V + 1; v >= 1; v--) {
        dp[N][v] = INF;
    }
    dp[N][0] = 0;
    for (int n = N - 1; n >= 0; n--) {
        for (int v = V + 1; v >= 0; v--) {
            if (v <= p[n].first) {
                dp[n][v] = min(dp[n + 1][v], p[n].second);
            } else {
                dp[n][v] = min(dp[n + 1][v], dp[n + 1][v - p[n].first] + p[n].second);
            }
        }
    }
    int vmin = dp[0][V];
    int vmax = dp[0][V + 1];
    cout << max(1, vmin) << endl;
    if (vmax == INF) {
        cout << "inf" << endl;
    } else {
        cout << vmax - 1 << endl;
    }

    return 0;
}
0