結果

問題 No.527 ナップサック容量問題
ユーザー くれちーくれちー
提出日時 2017-06-10 04:14:20
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,203 bytes
コンパイル時間 493 ms
コンパイル使用メモリ 32,532 KB
実行使用メモリ 41,124 KB
最終ジャッジ日時 2023-10-24 05:55:54
合計ジャッジ時間 2,086 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 14 ms
28,836 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 17 ms
37,028 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 7 ms
14,500 KB
testcase_15 AC 10 ms
22,692 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 9 ms
18,596 KB
testcase_18 AC 9 ms
20,644 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 7 ms
16,548 KB
testcase_21 AC 19 ms
41,124 KB
testcase_22 AC 12 ms
28,836 KB
testcase_23 AC 18 ms
41,124 KB
testcase_24 AC 5 ms
10,404 KB
testcase_25 AC 13 ms
28,836 KB
testcase_26 AC 11 ms
24,740 KB
testcase_27 AC 11 ms
24,740 KB
testcase_28 AC 10 ms
22,692 KB
testcase_29 AC 18 ms
41,124 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 8 ms
18,596 KB
testcase_32 AC 10 ms
22,692 KB
testcase_33 AC 9 ms
20,644 KB
testcase_34 AC 11 ms
24,740 KB
testcase_35 AC 11 ms
24,740 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 8 ms
18,596 KB
testcase_38 AC 10 ms
22,692 KB
testcase_39 AC 9 ms
20,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#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 RREP1(i, n) for (ll i = n; i >= 1; i--)
#define FOR(i, a, b, c) for (ll i = a; i <= b; i += c)
#define RFOR(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b)
#define MAX3(a, b, c) MAX(MAX(a, b), c)
#define MIN3(a, b, c) MIN(MIN(a, b), c)
#define SORT(t, a, n) qsort(a, n, sizeof(t), ({ int _f (const void *_a, const void *_b) { return *(t *)_a - *(t *)_b; } _f; }))
#define RSORT(t, a, n) qsort(a, n, sizeof(t), ({ int _f (const void *_a, const void *_b) { return *(t *)_b - *(t *)_a; } _f; }))
#define SUM(a, n) ({ ll _s = 0; REP(_i, n) _s += a[_i]; _s; })
#define PROD(a, n) ({ ll _p = 1; REP(_i, n) _p *= a[_i]; _p; })
#define CNT(x, a, n) ({ ll _c = 0; REP(_i, n) if (a[_i] == x) _c++; _c; })
#define MAXM(t, a, n) ({ t _m = a[0]; REP(_i, n) _m = MAX(_m, a[_i]); _m; })
#define MINM(t, a, n) ({ t _m = a[0]; REP(_i, n) _m = MIN(_m, a[_i]); _m; })
#define INF 1073709056
#define LLINF 4611686016279904256LL
typedef long long ll;

int n, v;
int vs[114], ws[114];
int dp[114][100001];

int main() {
    scanf("%d", &n);
    REP1(i, n) scanf("%d %d", &vs[i], &ws[i]);
    scanf("%d", &v);

    if (SUM((vs + 1), n) == v) {
        int min = SUM((ws + 1), n);
        printf("%d\ninf\n", min);
        return 0;
    }
    int min = 1, max = 100000;
    REP1(i, n) {
        REP(j, max + 1) {
            if (ws[i] <= j) dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - ws[i]] + vs[i]);
            else dp[i][j] = dp[i - 1][j];
        }
    }
    while (true) {
        int w = (min + max) / 2;
        int v_tmp = dp[n][w];
        if (v_tmp < v) min = w;
        else if (v_tmp > v) max = w;
        else break;
    }
    FOR(i, min, 100000, 1) {
        if (dp[n][i] > v) {
            max = i - 1;
            break;
        }
    }
    RFOR(i, max, 1, 1) {
        if (dp[n][i] < v) {
            min = i + 1;
            break;
        }
    }
    printf("%d\n%d\n", min, max);
    return 0;
}
0