結果
| 問題 | 
                            No.2665 Minimize Inversions of Deque
                             | 
                    
| コンテスト | |
| ユーザー | 
                             eve__fuyuki
                         | 
                    
| 提出日時 | 2024-04-01 10:02:10 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,025 bytes | 
| コンパイル時間 | 2,182 ms | 
| コンパイル使用メモリ | 200,516 KB | 
| 最終ジャッジ日時 | 2025-02-20 18:46:07 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 34 WA * 6 | 
ソースコード
#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
using namespace std;
using namespace atcoder;
void fast_io() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}
void solve() {
    int n;
    cin >> n;
    fenwick_tree<int> fw(n + 2);
    deque<int> ans;
    int inv = 0;
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        int l_cnt = fw.sum(0, p), r_cnt = fw.sum(p + 1, n + 2);
        if (l_cnt < r_cnt) {
            ans.push_front(p);
            inv += l_cnt;
        } else if (l_cnt > r_cnt) {
            ans.push_back(p);
            inv += r_cnt;
        } else if (p < ans.front()) {
            ans.push_front(p);
            inv += l_cnt;
        } else {
            ans.push_back(p);
            inv += r_cnt;
        }
        fw.add(p, 1);
    }
    cout << inv << "\n";
    for (int i = 0; i < n; i++) {
        cout << ans[i] << (i + 1 == n ? "\n" : " ");
    }
}
int main() {
    fast_io();
    int t;
    cin >> t;
    for (; t--;) {
        solve();
    }
}
            
            
            
        
            
eve__fuyuki