結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー suisensuisen
提出日時 2022-04-22 14:18:27
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,626 bytes
コンパイル時間 2,669 ms
コンパイル使用メモリ 79,932 KB
実行使用メモリ 113,328 KB
最終ジャッジ日時 2023-09-09 19:42:11
合計ジャッジ時間 34,155 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,904 KB
testcase_01 RE -
testcase_02 AC 123 ms
55,744 KB
testcase_03 AC 215 ms
55,768 KB
testcase_04 RE -
testcase_05 AC 130 ms
55,796 KB
testcase_06 AC 198 ms
56,012 KB
testcase_07 AC 193 ms
56,048 KB
testcase_08 AC 136 ms
55,876 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 219 ms
56,116 KB
testcase_12 AC 126 ms
55,696 KB
testcase_13 RE -
testcase_14 AC 186 ms
55,752 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 158 ms
55,984 KB
testcase_18 AC 125 ms
55,472 KB
testcase_19 AC 181 ms
56,396 KB
testcase_20 AC 121 ms
56,016 KB
testcase_21 AC 175 ms
56,472 KB
testcase_22 AC 173 ms
56,104 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 127 ms
55,668 KB
testcase_26 AC 190 ms
56,156 KB
testcase_27 RE -
testcase_28 RE -
testcase_29 AC 1,557 ms
59,092 KB
testcase_30 AC 1,571 ms
58,748 KB
testcase_31 AC 1,423 ms
58,652 KB
testcase_32 AC 1,059 ms
59,008 KB
testcase_33 RE -
testcase_34 AC 1,389 ms
58,952 KB
testcase_35 AC 1,041 ms
59,144 KB
testcase_36 AC 1,370 ms
59,036 KB
testcase_37 AC 1,427 ms
59,232 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

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) {
        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);

        for (int i = n; i > 0; i--) {
            final int max_num = n;
            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