結果

問題 No.2665 Minimize Inversions of Deque
ユーザー suisensuisen
提出日時 2023-12-28 21:23:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,427 bytes
コンパイル時間 968 ms
コンパイル使用メモリ 83,260 KB
実行使用メモリ 13,480 KB
最終ジャッジ日時 2024-01-05 15:02:50
合計ジャッジ時間 9,037 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 KB
testcase_01 AC 102 ms
6,676 KB
testcase_02 AC 108 ms
6,676 KB
testcase_03 AC 167 ms
6,676 KB
testcase_04 AC 180 ms
6,676 KB
testcase_05 AC 146 ms
6,676 KB
testcase_06 AC 156 ms
6,676 KB
testcase_07 AC 126 ms
6,676 KB
testcase_08 AC 131 ms
6,676 KB
testcase_09 AC 117 ms
6,676 KB
testcase_10 AC 110 ms
6,676 KB
testcase_11 AC 108 ms
6,676 KB
testcase_12 AC 102 ms
6,676 KB
testcase_13 AC 117 ms
6,676 KB
testcase_14 AC 101 ms
6,676 KB
testcase_15 AC 134 ms
6,676 KB
testcase_16 AC 106 ms
6,676 KB
testcase_17 AC 145 ms
6,676 KB
testcase_18 AC 114 ms
6,676 KB
testcase_19 AC 15 ms
6,676 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// TLE: naive DFS

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

std::pair<long long, std::vector<int>> naive(int n, const std::vector<int>& p) {
    long long min_inv = 1LL << 60;
    std::vector<int> ans;

    std::deque<int> dq;
    auto dfs = [&](auto dfs, int i, long long inv) -> void {
        if (inv > min_inv) return;

        if (i == n) {
            std::vector<int> a(dq.begin(), dq.end());
            if (inv < min_inv or a < ans) {
                min_inv = inv;
                ans = std::move(a);
            }
            return;
        }

        const int v = p[i];
        int cnt_lt = 0, cnt_gt = 0;
        for (int e : dq) ++(e < v ? cnt_lt : cnt_gt);
        dq.push_front(v);
        dfs(dfs, i + 1, inv + cnt_lt);
        dq.pop_front();

        if (i == 0) return;

        dq.push_back(v);
        dfs(dfs, i + 1, inv + cnt_gt);
        dq.pop_back();
    };
    dfs(dfs, 0, 0LL);
    return { min_inv, ans };
}

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] = naive(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