結果

問題 No.527 ナップサック容量問題
ユーザー くれちーくれちー
提出日時 2017-06-09 23:15:58
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 2,256 bytes
コンパイル時間 1,259 ms
コンパイル使用メモリ 32,396 KB
実行使用メモリ 8,424 KB
最終ジャッジ日時 2023-10-24 01:27:21
合計ジャッジ時間 4,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

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[100], ws[100];

int knapsack(int w) {
    static int dp[100][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, n) == v) {
        int min = SUM(ws, 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