結果

問題 No.527 ナップサック容量問題
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-04-30 14:30:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,372 bytes
コンパイル時間 1,335 ms
コンパイル使用メモリ 78,244 KB
実行使用メモリ 42,824 KB
最終ジャッジ日時 2023-08-28 17:17:12
合計ジャッジ時間 3,151 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 9 ms
9,928 KB
testcase_06 AC 34 ms
32,292 KB
testcase_07 AC 23 ms
21,828 KB
testcase_08 AC 7 ms
8,012 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 35 ms
32,648 KB
testcase_11 AC 18 ms
17,524 KB
testcase_12 AC 9 ms
10,628 KB
testcase_13 AC 35 ms
33,992 KB
testcase_14 AC 6 ms
6,988 KB
testcase_15 AC 12 ms
13,352 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 10 ms
10,152 KB
testcase_18 AC 11 ms
12,076 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 7 ms
7,752 KB
testcase_21 AC 46 ms
42,824 KB
testcase_22 AC 20 ms
20,720 KB
testcase_23 AC 42 ms
40,780 KB
testcase_24 AC 3 ms
4,700 KB
testcase_25 AC 20 ms
19,528 KB
testcase_26 AC 15 ms
16,292 KB
testcase_27 AC 16 ms
16,640 KB
testcase_28 AC 12 ms
13,208 KB
testcase_29 AC 46 ms
42,144 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 10 ms
10,304 KB
testcase_32 AC 14 ms
14,224 KB
testcase_33 AC 10 ms
10,912 KB
testcase_34 AC 15 ms
14,792 KB
testcase_35 AC 14 ms
14,912 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 8 ms
9,968 KB
testcase_38 AC 12 ms
13,552 KB
testcase_39 AC 11 ms
12,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <math.h>
#include <set>
#include <cstdio>
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define P pair<int, int>

#define MOD 1000000007
#define INF 1012345678
#define NINF (-2147483647-1)
#define LLINF 9223372036854775807
using ll = long long;
using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, V;
    cin >> N;
    vector<int> v(N), w(N);
    for (int i = 0; i < N; i++)
    {
        cin >> v[i] >> w[i];
    }
    vector<vector<int>> dp(N + 1, vector<int>(1000 * N, 0));
    for (int i = 1; i <= N; i++)
    {
        for (int j = 0; j < 1000 * N; j++)
        {
            if (j - w[i - 1] >= 0) {
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
            }
            else {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    cin >> V;
    int ans = lower_bound(ALL(dp[N]), V) - dp[N].begin();
    if (ans == 0) {
        cout << 1 << endl;
    }
    else {
        cout << ans << endl;
    }
    if (dp[N][1000 * N - 1] != V) {
        cout << upper_bound(ALL(dp[N]), V) - dp[N].begin() - 1 << endl;
    }
    else {
        cout << "inf" << endl;
    }
    getchar(); getchar();
}
0