結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー suisensuisen
提出日時 2022-04-22 14:36:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,599 ms / 10,000 ms
コード長 2,610 bytes
コンパイル時間 3,147 ms
コンパイル使用メモリ 77,068 KB
実行使用メモリ 145,324 KB
最終ジャッジ日時 2023-09-26 18:53:01
合計ジャッジ時間 36,628 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,524 KB
testcase_01 AC 125 ms
55,584 KB
testcase_02 AC 123 ms
56,064 KB
testcase_03 AC 155 ms
57,788 KB
testcase_04 AC 143 ms
55,876 KB
testcase_05 AC 131 ms
55,912 KB
testcase_06 AC 151 ms
57,844 KB
testcase_07 AC 143 ms
57,928 KB
testcase_08 AC 132 ms
55,864 KB
testcase_09 AC 178 ms
59,128 KB
testcase_10 AC 151 ms
57,780 KB
testcase_11 AC 179 ms
59,232 KB
testcase_12 AC 125 ms
55,920 KB
testcase_13 AC 183 ms
58,676 KB
testcase_14 AC 149 ms
57,596 KB
testcase_15 AC 125 ms
55,848 KB
testcase_16 AC 152 ms
57,772 KB
testcase_17 AC 144 ms
55,884 KB
testcase_18 AC 126 ms
55,900 KB
testcase_19 AC 143 ms
55,592 KB
testcase_20 AC 121 ms
53,872 KB
testcase_21 AC 141 ms
55,788 KB
testcase_22 AC 137 ms
55,768 KB
testcase_23 AC 152 ms
57,604 KB
testcase_24 AC 127 ms
57,728 KB
testcase_25 AC 131 ms
55,708 KB
testcase_26 AC 150 ms
57,924 KB
testcase_27 AC 138 ms
56,036 KB
testcase_28 AC 361 ms
61,728 KB
testcase_29 AC 362 ms
61,772 KB
testcase_30 AC 359 ms
61,520 KB
testcase_31 AC 354 ms
61,748 KB
testcase_32 AC 344 ms
62,000 KB
testcase_33 AC 344 ms
61,728 KB
testcase_34 AC 340 ms
63,644 KB
testcase_35 AC 342 ms
59,544 KB
testcase_36 AC 333 ms
61,488 KB
testcase_37 AC 361 ms
61,560 KB
testcase_38 AC 3,583 ms
142,484 KB
testcase_39 AC 3,181 ms
145,152 KB
testcase_40 AC 3,350 ms
144,768 KB
testcase_41 AC 3,478 ms
145,236 KB
testcase_42 AC 3,599 ms
145,324 KB
testcase_43 AC 3,476 ms
145,292 KB
testcase_44 AC 3,541 ms
144,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        var sc = new Scanner(System.in);
        var pw = new PrintWriter(System.out, false);
        solve(sc, pw);
        sc.close();
        pw.flush();
        pw.close();
    }

    public static final long INF = 1L << 60;

    public static void solve(Scanner sc, PrintWriter pw) {
        final int n = Integer.parseInt(sc.next());
        var c = new int[n + 1];
        var v = new long[n + 1];
        for (int i = 1; i <= n; i++) {
            c[i] = Integer.parseInt(sc.next());
            v[i] = Integer.parseInt(sc.next());
        }

        var dp = new long[n + 1][n + 1];
        for (int i = 0; i <= n; i++) Arrays.fill(dp[i], -INF);
        dp[0][0] = 0;

        for (int i = n; i > 0; i--) {
            final int max_num = n / i;
            final int max_sum = n;

            for (int num = 0; num <= max_num; num++) {
                final int sum_r = num > 0 ? i - 1 : max_sum;
                for (int sum = 0; sum <= sum_r; sum++) {
                    final int max_p = Math.min(max_num - num, (max_sum - sum) / i);
                    var init = new long[max_p + 1];
                    for (int p = 0; p <= max_p; ++p) {
                        init[p] = dp[num + p][sum + i * p] - (num + p) * v[i];
                    }
                    var seg = new RangeMaxSegmentTree(init);
                    for (int p = 0; p <= max_p; ++p) {
                        dp[num + p][sum + i * p] = seg.max(Math.max(0, p - c[i]), p + 1) + (num + p) * v[i];
                    }
                }
            }
        }
        
        for (int i = 1; i <= n; i++) {
            pw.println(Arrays.stream(dp[i]).max().getAsLong());
        }
    }

    public static class RangeMaxSegmentTree {
        private int _n;
        private long[] _dat;

        public RangeMaxSegmentTree(long[] dat) {
            _n = dat.length;
            _dat = new long[2 * _n];
            System.arraycopy(dat, 0, _dat, _n, _n);
            for (int i = _n - 1; i > 0; i--) {
                _dat[i] = Math.max(_dat[2 * i], _dat[2 * i + 1]);
            }
        }

        public long max(int l, int r) {
            long res = Long.MIN_VALUE;
            l += _n;
            r += _n;
            while (l < r) {
                if ((l & 1) != 0) res = Math.max(res, _dat[l++]);
                if ((r & 1) != 0) res = Math.max(res, _dat[--r]);
                l >>= 1;
                r >>= 1;
            }
            return res;
        }
    }
}
0