結果

問題 No.1867 Partitions and Inversions
ユーザー ForestedForested
提出日時 2022-03-02 11:36:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,094 ms / 5,000 ms
コード長 3,650 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 86,944 KB
実行使用メモリ 214,612 KB
最終ジャッジ日時 2023-09-23 05:33:05
合計ジャッジ時間 45,804 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1,066 ms
214,568 KB
testcase_03 AC 1,064 ms
214,492 KB
testcase_04 AC 1,058 ms
214,440 KB
testcase_05 AC 1,052 ms
214,572 KB
testcase_06 AC 1,078 ms
214,488 KB
testcase_07 AC 1,058 ms
214,568 KB
testcase_08 AC 1,052 ms
214,436 KB
testcase_09 AC 1,054 ms
214,584 KB
testcase_10 AC 1,050 ms
214,612 KB
testcase_11 AC 1,056 ms
214,492 KB
testcase_12 AC 1,064 ms
214,440 KB
testcase_13 AC 1,064 ms
214,444 KB
testcase_14 AC 1,066 ms
214,584 KB
testcase_15 AC 1,075 ms
214,504 KB
testcase_16 AC 1,076 ms
214,568 KB
testcase_17 AC 1,060 ms
214,492 KB
testcase_18 AC 1,080 ms
214,496 KB
testcase_19 AC 1,086 ms
214,492 KB
testcase_20 AC 1,073 ms
214,500 KB
testcase_21 AC 1,056 ms
214,508 KB
testcase_22 AC 1,082 ms
214,440 KB
testcase_23 AC 1,083 ms
214,564 KB
testcase_24 AC 1,073 ms
214,580 KB
testcase_25 AC 1,089 ms
214,564 KB
testcase_26 AC 1,071 ms
214,496 KB
testcase_27 AC 1,092 ms
214,564 KB
testcase_28 AC 1,088 ms
214,436 KB
testcase_29 AC 1,092 ms
214,496 KB
testcase_30 AC 1,092 ms
214,440 KB
testcase_31 AC 1,094 ms
214,512 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 13 ms
8,852 KB
testcase_39 AC 14 ms
8,992 KB
testcase_40 AC 13 ms
8,840 KB
testcase_41 AC 13 ms
8,908 KB
testcase_42 AC 14 ms
8,928 KB
testcase_43 AC 352 ms
97,012 KB
testcase_44 AC 363 ms
97,084 KB
testcase_45 AC 362 ms
97,024 KB
testcase_46 AC 368 ms
97,012 KB
testcase_47 AC 374 ms
96,960 KB
testcase_48 AC 359 ms
97,080 KB
testcase_49 AC 379 ms
97,012 KB
testcase_50 AC 372 ms
97,284 KB
testcase_51 AC 365 ms
97,020 KB
testcase_52 AC 356 ms
96,956 KB
testcase_53 AC 350 ms
97,084 KB
testcase_54 AC 357 ms
96,960 KB
testcase_55 AC 366 ms
97,032 KB
testcase_56 AC 366 ms
97,020 KB
testcase_57 AC 367 ms
97,088 KB
testcase_58 AC 383 ms
97,104 KB
testcase_59 AC 359 ms
97,008 KB
testcase_60 AC 356 ms
97,020 KB
testcase_61 AC 358 ms
97,032 KB
testcase_62 AC 381 ms
97,092 KB
testcase_63 AC 319 ms
214,588 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 2 ms
4,376 KB
testcase_66 AC 1 ms
4,376 KB
testcase_67 AC 317 ms
214,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

using usize = std::size_t;
template <typename T>
using Vec = std::vector<T>;

Vec<Vec<usize>> compute_inverse(const usize n, const Vec<usize> p) {
    Vec<Vec<usize>> sum(n + 1, Vec<usize>(n + 1, 0));
    for (usize i = 0; i < n; ++i) {
        ++sum[i + 1][p[i] + 1];
    }
    for (usize i = 0; i < n; ++i) {
        for (usize j = 0; j <= n; ++j) {
            sum[i + 1][j] += sum[i][j];
        }
    }
    for (usize i = 0; i <= n; ++i) {
        for (usize j = 0; j < n; ++j) {
            sum[i][j + 1] += sum[i][j];
        }
    }
    
    Vec<Vec<usize>> inv(n, Vec<usize>(n + 1, 0));
    for (usize i = 0; i < n; ++i) {
        usize s = 0;
        for (usize j = i; j < n; ++j) {
            s += sum[j][n] - sum[j][p[j]] - sum[i][n] + sum[i][p[j]];
            inv[i][j + 1] = s;
        }
    }
    
    return inv;
}

class Manager {
    // f(x) = inv[i][x] + offset (i < x)
    // max range: [?, limit]
    struct Func {
        usize i;
        usize offset;
        usize limit;
        
        Func(usize _i, usize _o, usize _l) : i(_i), offset(_o), limit(_l) {}
    };
    
    const usize n;
    const Vec<Vec<usize>> inv;
    Vec<Func> funcs;
    
public:
    Manager(usize _n, Vec<Vec<usize>> _i) : n(_n), inv(std::move(_i)), funcs() {}
    
    void add(const usize i, const usize offset) {
        if (funcs.empty()) {
            funcs.emplace_back(i, offset, n);
            return;
        }
        while (funcs.back().limit <= i) {
            funcs.pop_back();
        }
        if (const Func &f = funcs.back(); offset <= inv[f.i][i + 1] + f.offset) {
            return;
        }
        while (!funcs.empty()) {
            const Func &f = funcs.back();
            if (inv[f.i][f.limit] + f.offset < inv[i][f.limit] + offset) {
                funcs.pop_back();
            } else {
                usize ok = i + 1;
                usize ng = f.limit;
                while (ng - ok > 1) {
                    const usize mid = (ok + ng) / 2;
                    if (inv[f.i][mid] + f.offset < inv[i][mid] + offset) {
                        ok = mid;
                    } else {
                        ng = mid;
                    }
                }
                funcs.emplace_back(i, offset, ok);
                return;
            }
        }
        funcs.emplace_back(i, offset, n);
    }
    
    usize max(const usize x) {
        while (funcs.back().limit < x) {
            funcs.pop_back();
        }
        const Func &f = funcs.back();
        return inv[f.i][x] + f.offset;
    }
    
    void clear() {
        funcs.clear();
    }
};

Vec<usize> solve(const usize n, Vec<usize> p) {
    const Vec<Vec<usize>> inv = compute_inverse(n, std::move(p));
    Manager manager(n, inv);
    
    Vec<Vec<usize>> dp(n + 1, Vec<usize>(n + 1));
    for (usize i = 1; i <= n; ++i) {
        dp[1][i] = inv[0][i];
    }
    for (usize k = 2; k <= n; ++k) {
        manager.add(k - 1, dp[k - 1][k - 1]);
        for (usize i = k; i <= n; ++i) {
            dp[k][i] = manager.max(i);
            if (i != n) {
                manager.add(i, dp[k - 1][i]);
            }
        }
        manager.clear();
    }
    
    Vec<usize> ans(n + 1, 0);
    for (usize i = 1; i <= n; ++i) {
        ans[i] = inv[0][n] - dp[i][n];
    }
    return ans;
}

int main() {
    usize n;
    std::cin >> n;
    Vec<usize> p(n);
    for (usize &ele : p) {
        std::cin >> ele;
        --ele;
    }
    
    const Vec<usize> ans = solve(n, std::move(p));
    for (usize i = 1; i <= n; ++i) {
        std::cout << ans[i] << '\n';
    }
}
0