結果

問題 No.2665 Minimize Inversions of Deque
ユーザー みここみここ
提出日時 2024-03-08 21:11:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 958 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 81,832 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 21:11:35
合計ジャッジ時間 6,878 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 131 ms
6,548 KB
testcase_02 AC 126 ms
6,548 KB
testcase_03 AC 122 ms
6,676 KB
testcase_04 AC 120 ms
6,676 KB
testcase_05 AC 120 ms
6,676 KB
testcase_06 AC 121 ms
6,676 KB
testcase_07 AC 121 ms
6,676 KB
testcase_08 AC 120 ms
6,676 KB
testcase_09 AC 120 ms
6,676 KB
testcase_10 AC 120 ms
6,676 KB
testcase_11 AC 120 ms
6,676 KB
testcase_12 AC 121 ms
6,676 KB
testcase_13 AC 121 ms
6,676 KB
testcase_14 AC 120 ms
6,676 KB
testcase_15 AC 121 ms
6,676 KB
testcase_16 AC 120 ms
6,676 KB
testcase_17 AC 120 ms
6,676 KB
testcase_18 AC 120 ms
6,676 KB
testcase_19 AC 19 ms
6,676 KB
testcase_20 AC 3 ms
6,676 KB
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 3 ms
6,676 KB
testcase_23 AC 4 ms
6,676 KB
testcase_24 AC 4 ms
6,676 KB
testcase_25 AC 4 ms
6,676 KB
testcase_26 AC 3 ms
6,676 KB
testcase_27 AC 4 ms
6,676 KB
testcase_28 AC 4 ms
6,676 KB
testcase_29 AC 4 ms
6,676 KB
testcase_30 AC 96 ms
6,676 KB
testcase_31 AC 88 ms
6,676 KB
testcase_32 AC 90 ms
6,676 KB
testcase_33 AC 92 ms
6,676 KB
testcase_34 AC 90 ms
6,676 KB
testcase_35 AC 93 ms
6,676 KB
testcase_36 AC 93 ms
6,676 KB
testcase_37 AC 88 ms
6,676 KB
testcase_38 AC 91 ms
6,676 KB
testcase_39 AC 95 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <deque>
#include <atcoder/fenwicktree>
using namespace std;
using namespace atcoder;
typedef long long ll;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int p[200005];
        for (int i = 0; i < n; i++)
        {
            cin >> p[i];
            p[i]--;
        }
        deque<int> deq;
        fenwick_tree<int> bit(n);
        ll ans = 0;
        for (int i = 0; i < n; i++)
        {
            if (bit.sum(0, p[i]) < bit.sum(p[i], n))
            {
                deq.push_front(p[i]);
            }
            else
            {
                deq.push_back(p[i]);
            }
            ans += min(bit.sum(0, p[i]), bit.sum(p[i], n));
            bit.add(p[i], 1);
        }
        cout << ans << endl;
        while (deq.size())
        {
            cout << deq.front() + 1 << " ";
            deq.pop_front();
        }
        cout << endl;
    }
}
0