結果

問題 No.2029 Swap Min Max Min
ユーザー milanis48663220milanis48663220
提出日時 2022-08-05 23:18:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,015 bytes
コンパイル時間 1,304 ms
コンパイル使用メモリ 124,640 KB
実行使用メモリ 10,436 KB
最終ジャッジ日時 2023-10-14 01:09:21
合計ジャッジ時間 5,204 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 16 ms
4,940 KB
testcase_06 AC 34 ms
7,704 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 10 ms
4,372 KB
testcase_11 WA -
testcase_12 AC 22 ms
5,852 KB
testcase_13 WA -
testcase_14 AC 50 ms
8,984 KB
testcase_15 WA -
testcase_16 AC 49 ms
8,924 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 49 ms
9,064 KB
testcase_20 AC 51 ms
8,964 KB
testcase_21 AC 49 ms
9,088 KB
testcase_22 AC 48 ms
9,060 KB
testcase_23 AC 52 ms
10,164 KB
testcase_24 AC 50 ms
10,236 KB
testcase_25 AC 36 ms
9,160 KB
testcase_26 AC 35 ms
9,040 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 WA -
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 1 ms
4,356 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 68 ms
9,352 KB
testcase_39 AC 48 ms
9,068 KB
testcase_40 AC 43 ms
8,496 KB
testcase_41 AC 72 ms
9,836 KB
testcase_42 AC 72 ms
9,820 KB
testcase_43 AC 46 ms
8,880 KB
testcase_44 AC 78 ms
9,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

template <typename T>
struct bit{
    int n;
    vector<T> data;

    bit(int n_){
        n = 1;
        while(n < n_) n *= 2;
        data = vector<T>(n+1);
        for(int i = 0; i <= n; i++) data[i] = 0;
    }
    
    T sum(int i){
        T ret = 0;
        while(i > 0){
            ret += data[i];
            i -= i&-i;
        }
        return ret;
    }

    void add(int i, T x){
        while(i <= n){
            data[i] += x;
            i += i&-i;
        }
    }
};

ll count_inversion(vector<int> v){
    int n = v.size();
    bit<ll> bt(n);
    ll ans = 0;
    for(int x: v){
        ans += bt.sum(n)-bt.sum(x);
        bt.add(x, 1);
    }
    return ans;
}

const ll inf = 1e18;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<int> a(n+1), pos(n+1);
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        pos[a[i]] = i;
    }
    ll ans = inf;
    if(n%2 == 0){
        vector<int> odd, even;
        for(int x = 1; x <= n/2; x++) even.push_back(pos[x]);
        for(int x = n/2+1; x <= n; x++) odd.push_back(pos[x]);
        sort(even.begin(), even.end());
        sort(odd.begin(), odd.end());
        vector<int> v;
        for(int i = 0; i < n/2; i++) {
            v.push_back(even[i]);
            v.push_back(odd[i]);
        }
        ans = count_inversion(v);
    }
    vector<int> odd, even;
    for(int x = 1; x <= n/2; x++) odd.push_back(pos[x]);
    for(int x = n/2+1; x <= n; x++) even.push_back(pos[x]);
    sort(even.begin(), even.end());
    sort(odd.begin(), odd.end());
    vector<int> v;
    for(int i = 0; i < n/2; i++) {
        v.push_back(even[i]);
        v.push_back(odd[i]);
    }
    if(n%2 == 1) v.push_back(even[n/2]);
    chmin(ans, count_inversion(v));
    cout << n/2 << ' ' << ans << endl;
}
0