結果

問題 No.2029 Swap Min Max Min
ユーザー ぷらぷら
提出日時 2022-08-05 21:36:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,707 bytes
コンパイル時間 2,098 ms
コンパイル使用メモリ 204,260 KB
実行使用メモリ 9,552 KB
最終ジャッジ日時 2023-10-14 00:03:43
合計ジャッジ時間 5,932 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 22 ms
4,892 KB
testcase_06 AC 47 ms
6,316 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 13 ms
4,352 KB
testcase_11 WA -
testcase_12 AC 30 ms
5,240 KB
testcase_13 WA -
testcase_14 AC 69 ms
7,868 KB
testcase_15 WA -
testcase_16 AC 67 ms
7,712 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 69 ms
7,612 KB
testcase_20 AC 69 ms
7,804 KB
testcase_21 AC 68 ms
8,012 KB
testcase_22 AC 67 ms
7,972 KB
testcase_23 AC 80 ms
9,284 KB
testcase_24 AC 79 ms
9,212 KB
testcase_25 AC 69 ms
7,868 KB
testcase_26 AC 68 ms
7,908 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 WA -
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 70 ms
8,212 KB
testcase_39 AC 67 ms
7,684 KB
testcase_40 AC 60 ms
7,312 KB
testcase_41 AC 74 ms
8,872 KB
testcase_42 AC 73 ms
8,732 KB
testcase_43 AC 65 ms
7,620 KB
testcase_44 AC 79 ms
9,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
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) {
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    T sum(int 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)-sum(l-1);
    }
    inline T operator[](int k) {
        return sum(k)-sum(k-1);
    }
};

long long cnt(int N,vector<int>P) {
    vector<int>Q(N);
    BinaryIndexedTree<int>bit(N);
    for(int i = 0; i < N; i++) {
        bit.add(i+1,1);
        Q[P[i]] = i;
    }
    long long ans = 0;
    for(int i = N-1; i >= 0; i--) {
        bit.add(Q[i]+1,-1);
        ans += bit.sum(Q[i]+1,N);
    }
    return ans;
}

int main() {
    int N;
    cin >> N;
    vector<int>A(N),B,C;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        if(A[i] <= N/2) {
            B.push_back(i);
        }
        else {
            C.push_back(i);
        }
    }
    cout << N/2 << " ";
    if(N%2 == 1) {
        vector<int>D;
        for(int i = 0; i < N/2; i++) {
            D.push_back(C[i]);
            D.push_back(B[i]);
        }
        D.push_back(C[N/2]);
        cout << cnt(N,D) << endl;
    }
    else {
        vector<int>D,E;
        for(int i = 0; i < N/2; i++) {
            D.push_back(C[i]);
            D.push_back(B[i]);
            E.push_back(B[i]);
            E.push_back(C[i]);
        }
        cout << min(cnt(N,D),cnt(N,E)) << endl;
    }
}
0