結果

問題 No.2665 Minimize Inversions of Deque
ユーザー ぷらぷら
提出日時 2024-03-08 23:41:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,136 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 143,412 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 23:41:19
合計ジャッジ時間 5,550 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 37 ms
6,548 KB
testcase_02 AC 37 ms
6,676 KB
testcase_03 AC 37 ms
6,676 KB
testcase_04 AC 37 ms
6,676 KB
testcase_05 AC 36 ms
6,676 KB
testcase_06 AC 37 ms
6,676 KB
testcase_07 AC 35 ms
6,676 KB
testcase_08 AC 36 ms
6,676 KB
testcase_09 AC 37 ms
6,676 KB
testcase_10 AC 37 ms
6,676 KB
testcase_11 AC 37 ms
6,676 KB
testcase_12 AC 36 ms
6,676 KB
testcase_13 AC 36 ms
6,676 KB
testcase_14 AC 37 ms
6,676 KB
testcase_15 AC 37 ms
6,676 KB
testcase_16 AC 36 ms
6,676 KB
testcase_17 AC 36 ms
6,676 KB
testcase_18 AC 36 ms
6,676 KB
testcase_19 AC 7 ms
6,676 KB
testcase_20 AC 3 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 3 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 3 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 46 ms
6,676 KB
testcase_31 AC 44 ms
6,676 KB
testcase_32 AC 43 ms
6,676 KB
testcase_33 AC 45 ms
6,676 KB
testcase_34 AC 42 ms
6,676 KB
testcase_35 AC 44 ms
6,676 KB
testcase_36 AC 43 ms
6,676 KB
testcase_37 AC 42 ms
6,676 KB
testcase_38 AC 44 ms
6,676 KB
testcase_39 AC 46 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

template <typename T>
struct BinaryIndexedTree {
    int N;
    vector<T>bit;
    BinaryIndexedTree(){}
    BinaryIndexedTree(int x) {
        N = x;
        bit.resize(x+1);
    }
    void add(int x,T y) {
        x++;
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    T sum(int x) {
        x++;
        T res = 0;
        while(x) {
            res += bit[x];
            x -= x&-x;
        }
        return res;
    }
    inline T sum(int l, int r) {//[l,r)
        return sum(r-1)-sum(l-1);
    }
    inline T operator[](int k) {
        return sum(k)-sum(k-1);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int>p(n);
        BinaryIndexedTree<int>bit(n+1);
        deque<int>ans;
        long long fans = 0;
        for(int i = 0; i < n; i++) {
            cin >> p[i];
            int a = bit.sum(0,p[i]);
            int b = bit.sum(p[i],n+1);
            bit.add(p[i],1);
            fans += min(a,b);
            if(a < b) {
                ans.push_front(p[i]);
            }
            else if(b > a) {
                ans.push_back(p[i]);
            }
            else if(ans.empty() || ans.front() > p[i]) {
                ans.push_front(p[i]);
            }
            else {
                ans.push_back(p[i]);
            }
        }
        cout << fans << "\n";
        while(!ans.empty()) {
            cout << ans.front() << ((ans.size() == 1)?"\n":" ");
            ans.pop_front();
        }
    }
}
0