結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー suisensuisen
提出日時 2022-04-22 14:20:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,630 bytes
コンパイル時間 2,337 ms
コンパイル使用メモリ 77,716 KB
実行使用メモリ 112,316 KB
最終ジャッジ日時 2023-09-09 19:42:46
合計ジャッジ時間 33,435 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,788 KB
testcase_01 AC 127 ms
55,808 KB
testcase_02 AC 126 ms
55,708 KB
testcase_03 AC 219 ms
55,896 KB
testcase_04 AC 191 ms
55,700 KB
testcase_05 AC 131 ms
55,796 KB
testcase_06 AC 196 ms
56,076 KB
testcase_07 AC 197 ms
55,996 KB
testcase_08 AC 138 ms
55,452 KB
testcase_09 AC 195 ms
56,068 KB
testcase_10 AC 210 ms
53,956 KB
testcase_11 AC 219 ms
56,020 KB
testcase_12 AC 127 ms
55,464 KB
testcase_13 AC 167 ms
55,904 KB
testcase_14 AC 192 ms
55,964 KB
testcase_15 AC 124 ms
56,068 KB
testcase_16 AC 188 ms
56,424 KB
testcase_17 AC 159 ms
55,896 KB
testcase_18 AC 124 ms
55,780 KB
testcase_19 AC 181 ms
56,052 KB
testcase_20 AC 124 ms
55,952 KB
testcase_21 AC 175 ms
56,012 KB
testcase_22 AC 171 ms
56,084 KB
testcase_23 AC 213 ms
56,064 KB
testcase_24 AC 127 ms
55,924 KB
testcase_25 AC 132 ms
55,468 KB
testcase_26 AC 200 ms
56,096 KB
testcase_27 AC 162 ms
56,192 KB
testcase_28 AC 1,394 ms
59,456 KB
testcase_29 AC 1,552 ms
58,568 KB
testcase_30 AC 1,580 ms
59,200 KB
testcase_31 AC 1,492 ms
59,120 KB
testcase_32 AC 1,036 ms
59,112 KB
testcase_33 AC 1,557 ms
58,960 KB
testcase_34 AC 1,396 ms
59,240 KB
testcase_35 AC 1,051 ms
59,088 KB
testcase_36 AC 1,373 ms
58,904 KB
testcase_37 AC 1,427 ms
59,180 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 + 1);

        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