結果

問題 No.527 ナップサック容量問題
ユーザー くれちーくれちー
提出日時 2017-06-09 23:18:40
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1,219 ms / 2,000 ms
コード長 2,268 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 32,396 KB
実行使用メモリ 41,032 KB
最終ジャッジ日時 2023-10-24 01:51:02
合計ジャッジ時間 5,532 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 5 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 67 ms
28,744 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 180 ms
36,936 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 113 ms
14,408 KB
testcase_15 AC 31 ms
22,600 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 296 ms
18,504 KB
testcase_18 AC 192 ms
20,552 KB
testcase_19 AC 5 ms
4,348 KB
testcase_20 AC 11 ms
16,456 KB
testcase_21 AC 26 ms
41,032 KB
testcase_22 AC 14 ms
28,744 KB
testcase_23 AC 19 ms
41,032 KB
testcase_24 AC 7 ms
10,312 KB
testcase_25 AC 13 ms
26,696 KB
testcase_26 AC 12 ms
24,648 KB
testcase_27 AC 12 ms
24,648 KB
testcase_28 AC 10 ms
22,600 KB
testcase_29 AC 20 ms
41,032 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 9 ms
18,504 KB
testcase_32 AC 10 ms
22,600 KB
testcase_33 AC 9 ms
20,552 KB
testcase_34 AC 11 ms
24,648 KB
testcase_35 AC 1,219 ms
24,648 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 25 ms
18,504 KB
testcase_38 AC 191 ms
22,600 KB
testcase_39 AC 557 ms
20,552 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 knapsack(int w) {
    static int dp[114][100001];
    REP1(i, n) {
        REP(j, w + 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];
        }
    }
    return dp[n][w];
}

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;
    while (true) {
        int w = (min + max) / 2;
        int v_tmp = knapsack(w);
        if (v_tmp < v) min = w;
        else if (v_tmp > v) max = w;
        else break;
    }
    FOR(i, min, 100000, 1) {
        if (knapsack(i) > v) {
            max = i - 1;
            break;
        }
    }
    RFOR(i, max, 1, 1) {
        if (knapsack(i) < v) {
            min = i + 1;
            break;
        }
    }
    printf("%d\n%d\n", min, max);
    return 0;
}
0