結果

問題 No.2029 Swap Min Max Min
ユーザー milanis48663220milanis48663220
提出日時 2022-08-05 23:28:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 3,303 bytes
コンパイル時間 1,236 ms
コンパイル使用メモリ 124,788 KB
実行使用メモリ 9,284 KB
最終ジャッジ日時 2023-10-14 01:28:15
合計ジャッジ時間 4,452 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 25 ms
6,444 KB
testcase_04 AC 8 ms
4,352 KB
testcase_05 AC 15 ms
5,000 KB
testcase_06 AC 33 ms
7,724 KB
testcase_07 AC 29 ms
6,564 KB
testcase_08 AC 42 ms
8,604 KB
testcase_09 AC 12 ms
4,612 KB
testcase_10 AC 9 ms
4,376 KB
testcase_11 AC 16 ms
5,424 KB
testcase_12 AC 20 ms
6,144 KB
testcase_13 AC 44 ms
8,820 KB
testcase_14 AC 47 ms
9,044 KB
testcase_15 AC 47 ms
8,880 KB
testcase_16 AC 47 ms
9,060 KB
testcase_17 AC 44 ms
8,824 KB
testcase_18 AC 48 ms
9,040 KB
testcase_19 AC 48 ms
9,004 KB
testcase_20 AC 47 ms
9,052 KB
testcase_21 AC 47 ms
8,964 KB
testcase_22 AC 47 ms
8,900 KB
testcase_23 AC 35 ms
9,096 KB
testcase_24 AC 33 ms
9,124 KB
testcase_25 AC 34 ms
9,132 KB
testcase_26 AC 33 ms
9,096 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 42 ms
8,716 KB
testcase_39 AC 47 ms
8,992 KB
testcase_40 AC 41 ms
8,460 KB
testcase_41 AC 44 ms
8,952 KB
testcase_42 AC 44 ms
9,068 KB
testcase_43 AC 44 ms
8,952 KB
testcase_44 AC 47 ms
9,284 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++) 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]);
        }
        ans = count_inversion(v);
        ll tmp = ans;
        for(int i = n-2; i >= 0; i-=2){
            // i, i+1を反転
            if(v[i] < v[i+1]){
                tmp++;
            }else{
                tmp--;
            }
            chmin(ans, tmp);
        }
    }else{
        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]);
        }
        v.push_back(even[n/2]);
        chmin(ans, count_inversion(v));
    }
    cout << n/2 << ' ' << ans << endl;
}
0