結果

問題 No.2665 Minimize Inversions of Deque
ユーザー t98slider
提出日時 2024-04-27 16:49:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 4,577 ms
コンパイル使用メモリ 253,484 KB
最終ジャッジ日時 2025-02-21 09:16:05
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    deque<int> dq;
    while(T--){
        int n, v, s;
        long long ans = 0;
        cin >> n;
        dq.clear();
        atcoder::fenwick_tree<int> fw(n);
        for(int i = 0; i < n; i++){
            cin >> v;
            s = fw.sum(0, --v);
            if(s < i - s){
                ans += s;
                dq.emplace_front(v);
            }else{
                ans += i - s;
                dq.emplace_back(v);
            }
            fw.add(v, 1);
        }
        cout << ans << '\n';
        for(int i = 0; i < n; i++){
            cout << dq[i] + 1 << (i + 1 == n ? '\n' : ' ');
        }
    }
}
0