結果

問題 No.2665 Minimize Inversions of Deque
ユーザー kokoseikokosei
提出日時 2024-03-08 21:46:41
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 956 bytes
コンパイル時間 2,665 ms
コンパイル使用メモリ 251,700 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 21:46:49
合計ジャッジ時間 6,918 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 59 ms
6,676 KB
testcase_02 AC 61 ms
6,676 KB
testcase_03 AC 55 ms
6,676 KB
testcase_04 AC 64 ms
6,676 KB
testcase_05 AC 58 ms
6,676 KB
testcase_06 AC 60 ms
6,676 KB
testcase_07 AC 61 ms
6,676 KB
testcase_08 AC 60 ms
6,676 KB
testcase_09 AC 57 ms
6,676 KB
testcase_10 AC 59 ms
6,676 KB
testcase_11 AC 56 ms
6,676 KB
testcase_12 AC 57 ms
6,676 KB
testcase_13 AC 59 ms
6,676 KB
testcase_14 AC 58 ms
6,676 KB
testcase_15 AC 57 ms
6,676 KB
testcase_16 AC 56 ms
6,676 KB
testcase_17 AC 57 ms
6,676 KB
testcase_18 AC 58 ms
6,676 KB
testcase_19 AC 10 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 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 3 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 39 ms
6,676 KB
testcase_31 AC 38 ms
6,676 KB
testcase_32 AC 38 ms
6,676 KB
testcase_33 AC 41 ms
6,676 KB
testcase_34 AC 36 ms
6,676 KB
testcase_35 AC 36 ms
6,676 KB
testcase_36 AC 37 ms
6,676 KB
testcase_37 AC 36 ms
6,676 KB
testcase_38 AC 37 ms
6,676 KB
testcase_39 AC 45 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
using namespace std;
using ll = long long;

int N;
int P[202020];
void solve(){
    cin >> N;
    atcoder::fenwick_tree<int> bt(N);
    deque<int> ans;
    ll x = 0;
    for(int i = 0;i < N;i++){
        int p; cin >> p;
        p--;
        int f = bt.sum(0, p), b = bt.sum(p, N);
        if(f > b){
            ans.push_back(p + 1);
        }else if(f < b){
            ans.push_front(p + 1);
        }else{
            if(ans.size() && ans.front() > p){
                ans.push_front(p + 1);
            }else{
                ans.push_back(p + 1);
            }
        }
        x += min(f, b);
        bt.add(p, 1);
    }
    cout << x << endl;
    for(int i = 0;i < N;i++){
        cout << ans.front() << " \n"[i + 1 == N];
        ans.pop_front();
    }
}
int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t; cin >> t;
    while(t--)solve();
    return 0;
}
0