結果

問題 No.2665 Minimize Inversions of Deque
ユーザー GOTKAKOGOTKAKO
提出日時 2024-03-08 21:21:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 2,088 bytes
コンパイル時間 2,353 ms
コンパイル使用メモリ 210,612 KB
実行使用メモリ 8,184 KB
最終ジャッジ日時 2024-03-08 21:21:41
合計ジャッジ時間 7,767 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 104 ms
6,548 KB
testcase_02 AC 101 ms
6,548 KB
testcase_03 AC 95 ms
6,548 KB
testcase_04 AC 95 ms
6,676 KB
testcase_05 AC 95 ms
6,676 KB
testcase_06 AC 126 ms
6,676 KB
testcase_07 AC 95 ms
6,676 KB
testcase_08 AC 96 ms
6,676 KB
testcase_09 AC 94 ms
6,676 KB
testcase_10 AC 94 ms
6,676 KB
testcase_11 AC 94 ms
6,676 KB
testcase_12 AC 95 ms
6,676 KB
testcase_13 AC 94 ms
6,676 KB
testcase_14 AC 119 ms
6,676 KB
testcase_15 AC 95 ms
6,676 KB
testcase_16 AC 95 ms
6,676 KB
testcase_17 AC 95 ms
6,676 KB
testcase_18 AC 95 ms
6,676 KB
testcase_19 AC 15 ms
6,676 KB
testcase_20 AC 4 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 3 ms
6,676 KB
testcase_26 AC 3 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 3 ms
6,676 KB
testcase_29 AC 3 ms
6,676 KB
testcase_30 AC 96 ms
8,056 KB
testcase_31 AC 84 ms
6,676 KB
testcase_32 AC 91 ms
6,676 KB
testcase_33 AC 90 ms
7,984 KB
testcase_34 AC 83 ms
6,676 KB
testcase_35 AC 81 ms
8,184 KB
testcase_36 AC 79 ms
8,184 KB
testcase_37 AC 82 ms
6,676 KB
testcase_38 AC 85 ms
7,928 KB
testcase_39 AC 89 ms
8,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using SS = long long;
class SegmentTree{
    public:
    int siz = 1;
    vector<SS> dat;
 
    SS op(SS a, SS b){return a+b;}
    SS e(){return 0;}
    void renew (SS &a,SS x){
        //a = op(a,x);
        a = x;
        //その他.
    }
 
    void make(int N){
        while(siz < N) siz *= 2;
        dat.resize(siz*2,e());
    }
 
    void make2(int N,vector<SS> &A){
        make(N);
        for(int i=0; i<N; i++){
            int pos = i+siz;
            dat.at(pos) = A.at(i);
        }
        for(int i=siz-1; i>0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1));
    }
 
    void update(int pos,SS x){
        pos = pos+siz;
        renew(dat.at(pos),x);
        while(pos != 1){
            pos = pos/2;
            dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1));
        }
    }
 
    SS findans(int l, int r){
        SS ret = e();
        l += siz,r += siz;
        while(l < r){
            if(l&1) ret = op(ret,dat.at(l++));
            if(r&1) ret = op(ret,dat.at(--r));
            l >>= 1; r >>= 1;
        }
        return ret;
    }
 
    SS get(int pos){return dat.at(pos+siz);}
    SS rangeans(int l, int r){return findans(l,r);}
    SS allrange(){return findans(0,siz);}
 
    //ノーマルセグ木 二分探索は実装してない.
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T; cin >> T;
    while(T--){
        int N; cin >> N;
        long long answer = 0;
        SegmentTree Z; Z.make(N);

        int a1; cin >> a1; a1--;
        deque<int> Q; Q.push_back(a1);
        Z.update(a1,1);
        for(int i=1; i<N; i++){
            int a; cin >> a; a--;
            int fr = Z.rangeans(0,a),ba = Z.rangeans(a,N);
            if(fr >= ba) answer += ba,Q.push_back(a);
            else answer += fr,Q.push_front(a);
            Z.update(a,1);
        }
        
        cout << answer << endl;
        for(int i=0; i<N; i++){
            if(i) cout << " ";
            cout << Q.front()+1;
            Q.pop_front();
        }
        cout << endl;
        
    }
}
0