結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー suisensuisen
提出日時 2022-04-22 14:27:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,267 ms / 10,000 ms
コード長 2,640 bytes
コンパイル時間 4,841 ms
コンパイル使用メモリ 76,672 KB
実行使用メモリ 108,616 KB
最終ジャッジ日時 2023-09-26 18:40:32
合計ジャッジ時間 19,052 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,808 KB
testcase_01 AC 126 ms
55,868 KB
testcase_02 AC 123 ms
55,588 KB
testcase_03 AC 142 ms
55,780 KB
testcase_04 AC 139 ms
55,896 KB
testcase_05 AC 128 ms
55,788 KB
testcase_06 AC 143 ms
56,108 KB
testcase_07 AC 140 ms
55,792 KB
testcase_08 AC 132 ms
55,884 KB
testcase_09 AC 147 ms
56,040 KB
testcase_10 AC 138 ms
56,156 KB
testcase_11 AC 146 ms
56,244 KB
testcase_12 AC 123 ms
55,988 KB
testcase_13 AC 146 ms
55,900 KB
testcase_14 AC 142 ms
55,784 KB
testcase_15 AC 123 ms
56,092 KB
testcase_16 AC 141 ms
55,880 KB
testcase_17 AC 137 ms
55,928 KB
testcase_18 AC 124 ms
55,876 KB
testcase_19 AC 138 ms
56,164 KB
testcase_20 AC 123 ms
55,848 KB
testcase_21 AC 137 ms
56,148 KB
testcase_22 AC 134 ms
55,940 KB
testcase_23 AC 142 ms
55,884 KB
testcase_24 AC 123 ms
55,744 KB
testcase_25 AC 127 ms
55,496 KB
testcase_26 AC 141 ms
55,568 KB
testcase_27 AC 136 ms
55,784 KB
testcase_28 AC 278 ms
59,112 KB
testcase_29 AC 295 ms
59,024 KB
testcase_30 AC 280 ms
59,512 KB
testcase_31 AC 284 ms
57,164 KB
testcase_32 AC 276 ms
59,124 KB
testcase_33 AC 284 ms
59,728 KB
testcase_34 AC 274 ms
58,960 KB
testcase_35 AC 289 ms
59,132 KB
testcase_36 AC 292 ms
59,220 KB
testcase_37 AC 276 ms
59,548 KB
testcase_38 AC 1,052 ms
108,220 KB
testcase_39 AC 1,264 ms
108,260 KB
testcase_40 AC 1,267 ms
107,936 KB
testcase_41 AC 1,059 ms
108,616 KB
testcase_42 AC 1,050 ms
106,324 KB
testcase_43 AC 1,248 ms
108,120 KB
testcase_44 AC 1,226 ms
108,188 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;

        var sw = new SlidingWindowMaximum(n + 1);

        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);
                    sw.clear();
                    for (int p = 0; p <= max_p; ++p) {
                        sw.push_back(dp[num + p][sum + i * p] - (num + p) * v[i]);
                        if (p > c[i]) sw.pop_front();
                        dp[num + p][sum + i * p] = sw.query() + (num + p) * v[i];
                    }
                }
            }
        }
        
        for (int i = 1; i <= n; i++) {
            pw.println(Arrays.stream(dp[i]).max().getAsLong());
        }
    }

    public static class SlidingWindowMaximum {
        private int _l, _r;
        private int _ptr_l, _ptr_r;
        private long[] _val;
        private int[] _idx;

        public SlidingWindowMaximum(int cap) {
            _val = new long[cap];
            _idx = new int[cap];
            clear();
        }
    
        public void push_back(long v) {
            while (_ptr_l < _ptr_r && _val[_ptr_r - 1] < v) _ptr_r--;
            _val[_ptr_r] = v;
            _idx[_ptr_r] = _r;
            _ptr_r++;
            _r++;
        }
    
        public void pop_front() {
            if (_idx[_ptr_l] == _l) _ptr_l++;
            _l++;
        }
    
        public long query() {
            return _val[_ptr_l];
        }
    
        public void clear() {
            _l = _r = _ptr_l = _ptr_r = 0;
        }
    
        public int size() {
            return _r - _l;
        }
    }
}
0