結果

問題 No.2029 Swap Min Max Min
ユーザー milanis48663220milanis48663220
提出日時 2022-08-05 21:51:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,009 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 124,820 KB
実行使用メモリ 10,212 KB
最終ジャッジ日時 2023-10-14 00:08:19
合計ジャッジ時間 4,675 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 16 ms
4,948 KB
testcase_06 AC 34 ms
7,756 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 10 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 21 ms
5,900 KB
testcase_13 WA -
testcase_14 AC 50 ms
9,064 KB
testcase_15 WA -
testcase_16 AC 48 ms
8,972 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 49 ms
9,068 KB
testcase_20 AC 50 ms
8,952 KB
testcase_21 AC 49 ms
8,992 KB
testcase_22 AC 48 ms
9,064 KB
testcase_23 WA -
testcase_24 AC 49 ms
10,148 KB
testcase_25 AC 36 ms
9,032 KB
testcase_26 AC 36 ms
9,008 KB
testcase_27 WA -
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 WA -
testcase_33 AC 2 ms
4,348 KB
testcase_34 WA -
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 48 ms
9,180 KB
testcase_40 AC 44 ms
8,440 KB
testcase_41 AC 72 ms
9,676 KB
testcase_42 AC 73 ms
9,748 KB
testcase_43 AC 46 ms
8,940 KB
testcase_44 AC 77 ms
10,060 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]);
    ans = count_inversion(v);
    cout << n/2 << ' ' << ans << endl;
}
0