結果

問題 No.2665 Minimize Inversions of Deque
ユーザー suisensuisen
提出日時 2023-12-28 21:22:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,419 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 84,272 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-01-05 15:02:46
合計ジャッジ時間 5,633 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 35 ms
6,676 KB
testcase_02 AC 35 ms
6,676 KB
testcase_03 AC 34 ms
6,676 KB
testcase_04 AC 35 ms
6,676 KB
testcase_05 AC 34 ms
6,676 KB
testcase_06 AC 34 ms
6,676 KB
testcase_07 AC 35 ms
6,676 KB
testcase_08 AC 34 ms
6,676 KB
testcase_09 AC 33 ms
6,676 KB
testcase_10 AC 33 ms
6,676 KB
testcase_11 AC 34 ms
6,676 KB
testcase_12 AC 34 ms
6,676 KB
testcase_13 AC 34 ms
6,676 KB
testcase_14 AC 34 ms
6,676 KB
testcase_15 AC 34 ms
6,676 KB
testcase_16 AC 35 ms
6,676 KB
testcase_17 AC 34 ms
6,676 KB
testcase_18 AC 34 ms
6,676 KB
testcase_19 AC 7 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 3 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 3 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 3 ms
6,676 KB
testcase_30 AC 41 ms
6,676 KB
testcase_31 AC 39 ms
6,676 KB
testcase_32 AC 39 ms
6,676 KB
testcase_33 AC 41 ms
6,676 KB
testcase_34 AC 38 ms
6,676 KB
testcase_35 AC 39 ms
6,676 KB
testcase_36 AC 39 ms
6,676 KB
testcase_37 AC 38 ms
6,676 KB
testcase_38 AC 41 ms
6,676 KB
testcase_39 AC 42 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <deque>
#include <iostream>
#include <vector>

#include <atcoder/fenwicktree>

std::pair<long long, std::vector<int>> solve(int n, const std::vector<int>& p) {
    long long inv = 0;
    std::deque<int> a;

    atcoder::fenwick_tree<int> ft(n);
    for (int i = 0; i < n; ++i) {
        const int cnt_lt = ft.sum(0, p[i]);
        const int cnt_gt = i - cnt_lt;
        ft.add(p[i], 1);

        const bool ok_l = cnt_lt <= cnt_gt;
        const bool ok_r = cnt_lt >= cnt_gt;
        if (ok_l and ok_r) {
            if (a.empty() or a.front() < p[i]) {
                a.push_back(p[i]);
            } else {
                a.push_front(p[i]);
            }
        } else if (ok_l) {
            a.push_front(p[i]);
        } else if (ok_r) {
            a.push_back(p[i]);
        } else {
            assert(false);
        }
        inv += std::min(cnt_lt, cnt_gt);
    }
    return { inv, std::vector<int>(a.begin(), a.end()) };
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;

        std::vector<int> p(n);
        for (auto&& e : p) std::cin >> e, --e;

        auto [inv, a] = solve(n, p);

        std::cout << inv << '\n';
        for (int i = 0; i < n; ++i) {
            if (i) std::cout << ' ';
            std::cout << a[i] + 1;
        }
        std::cout << '\n';
    }
}
0