Problem:
What Goes Up Must Come Down
Several cards with numbers printed on them are lined up on the table.
We'd like to change their order so that first some are in non-decreasing order of the numbers on them, and the rest are in non-increasing order. For example, (1, 2, 3, 2, 1), (1, 1, 3, 4, 5, 9, 2), and (5, 3, 1) are acceptable orders, but (8, 7, 9) and (5, 3, 5, 3) are not.
To put it formally, with $n$ the number of cards and $b_i$ the number printed on the card at the $i$-th position ($1 \leq i \leq n$) after reordering, there should exist $k \in \{1, ... n\}$ such that ($b_i \leq b_{i+1} \forall _i \in \{1, ... k - 1\}$) and ($b_i \geq b_{i+1} \forall _i \in \{k, ..., n - 1\}$) hold.
For reordering, the only operation allowed at a time is to swap the positions of an adjacent card pair. We want to know the minimum number of swaps required to complete the reorder.
Input
The input consists of a single test case of the following format.
$n$
$a_1$ ... $a_n$
An integer $n$ in the first line is the number of cards ($1 \leq n \leq 100 000$). Integers $a_1$ through $a_n$ in the second line are the numbers printed on the cards, in the order of their original positions ($1 \leq a_i \leq 100 000$).
Output
Output in a line the minimum number of swaps required to reorder the cards as specified.
Sample Input 1
7
3 1 4 1 5 9 2
Sample Output 1
3
Sample Input 2
9
10 4 6 3 15 9 1 1 12
Sample Output 2
8
Sample Input 3
8
9 9 8 8 7 7 6 6
Sample Output 3
0
Sample Input 4
6
8 7 2 5 4 6
Sample Output 4
4

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
struct tree {
    vector<int> fw; int n;
    tree(int sz) : fw(sz + 1, 0), n(sz) {}
    void update(int id, int val) {
        for( ; id <= n; id += id & -id) fw[id] += val;
    }
    ll get(int id) {
        ll sum = 0;
        for( ; id > 0; id -= id & -id) sum += fw[id];
        return sum;
    }
};
int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int n; cin >> n;
    vector<ll> v(n), pre(n), suf(n); for(auto &i : v) cin >> i;
    vector<ll> com = v;
    sort(com.begin(), com.end());
    com.erase(unique(com.begin(), com.end()), com.end());
    int m = com.size();
    vector<int> id(n);
    for(int i = 0; i < n; i++) id[i] = lower_bound(com.begin(), com.end(), v[i]) - com.begin() + 1;
    tree fen(m);
    for(int i = 0; i < n; i++) {
        pre[i] = i - fen.get(id[i]);
        fen.update(id[i], 1);
    }
    fen.fw.assign(m + 1, 0);
    for(int i = n - 1; i >= 0; i--) {
        suf[i] = (n - 1 - i) - fen.get(id[i]);
        fen.update(id[i], 1);
    }
    ll ans = 0;
    for(int i = 0; i < n - 1; i++) ans += min(suf[i], pre[i]);
    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 6944
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Fenwick {
    int n;
    vector<long long> f;
    Fenwick(int n = 0) { reset(n); }
    void reset(int n_) { n = n_; f.assign(n + 1, 0); }
    void add(int i, long long v) { for (; i <= n; i += i & -i) f[i] += v; }
    long long sum(int i) const { long long s = 0; for (; i > 0; i -= i & -i) s += f[i]; return s; }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    if (!(cin >> n)) return 0;
    vector<long long> a(n);
    for (auto &x : a) cin >> x;

    // Nén giá trị về [1..m]
    vector<long long> vals = a;
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    const int m = (int)vals.size();
    vector<int> id(n);
    for (int i = 0; i < n; ++i)
        id[i] = int(lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin()) + 1; // 1..m

    // pre[i]: số phần tử trước i lớn hơn a[i]
    // suf[i]: số phần tử sau i nhỏ hơn a[i] (theo cùng logic của bạn)
    vector<long long> pre(n), suf(n);

    Fenwick bit(m);
    for (int i = 0; i < n; ++i) {
        pre[i] = i - bit.sum(id[i]);
        bit.add(id[i], 1);
    }

    bit.reset(m);
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = (n - 1 - i) - bit.sum(id[i]);
        bit.add(id[i], 1);
    }

    long long ans = 0;
    for (int i = 0; i < n - 1; ++i)
        ans += min(suf[i], pre[i]);

    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 7252
final_rank: 5

code_snippet: #include <bits/stdc++.h>

#define rep(i,A,B) for (int i = (A); i <= (B); i ++)
#define per(i,A,B) for (int i = (A); i >= (B); i --)
#define ll long long
#define pii pair <int, int>
#define pil pair <int, ll>
#define pll pair <ll, ll>

using namespace std;

const int N = 3e5 + 10;

struct BIT {
    int t[N], n;
    void init (int x) { n = x; for (int i = 1; i <= n; i ++) t[i] = 0; }
    int lowbit (int x) { return x & -x; }
    void add (int u, int d) { for (; u <= n; u += lowbit (u)) t[u] += d; }
    int qry (int l) { int ret = 0; for (; l; l -= lowbit (l)) ret += t[l]; return ret; }
} t;

int n;
int a[N];
vector <int> pos[N];

int main (void) {

    vector <int> vv;

    scanf ("%d", &n); t.init (n);
    for (int i = 1; i <= n; i ++) scanf ("%d", a + i), vv.push_back (a[i]), pos[a[i]].push_back (i), t.add (i, 1);

    sort (vv.begin (), vv.end ());
    vv.erase (unique (vv.begin (), vv.end ()), vv.end ());
    
    ll ans = 0;
    for (auto v : vv) {
        for (int i = 0; i < pos[v].size (); i ++) {
            int p = pos[v][i];
            ans += min ( t.qry (p - 1), t.qry (n) - t.qry (p) - ((int)pos[v].size () - (i + 1)) );
            t.add (p, -1);
        }
    }

    printf ("%lld\n", ans);

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 15268
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define vll vector<long long>
constexpr ll INF = 9009009009009009009LL;

template <typename T>
class segtree
{
    protected:
    const T E;
    vector<T> _data;
    const function<T(T, T)> _query;
    int _length;

    T _query_sub(int a, int b, int k, int l, int r) const {
        if(r <= a || b <= l){
            return E;
        }
        else if (a <= l && r <= b){
            return _data[k];
        }
        else{
            T vl = _query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = _query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return _query(vl, vr);
        }
    }

    public:
    segtree(int len, T e, function<T(T, T)> query) : E(e), _query(std::move(query)), _length(1) {
        while(_length < len){
            _length <<= 1;
        }
        _data.assign(_length * 2 - 1, E);
    }

    size_t size() const {return _length;}

    T &operator[](size_t i){
        if (i < 0 || i >= _length) throw out_of_range("Index out of range");
        return _data[i + _length - 1];
    }

    void build(){
        for (int i = _length - 2; i >= 0; --i){
            _data[i] = _query(_data[i * 2 + 1], _data[i * 2 + 2]);
        }
    }

    void update(int i, T value){
        if (i < 0 || i >= _length) throw out_of_range("Index out of range");
        i += _length - 1;
        _data[i] = value;
        while(i > 0){
            i = (i - 1) >> 1;
            _data[i] = _query(_data[i * 2 + 1], _data[i * 2 + 2]);
        }
    }

    T query(int a, int b) const {
        if(a < 0 || b < 0 || a >= _length || b > _length)throw out_of_range("Index out of range");
        return _query_sub(a, b, 0, 0, _length);
    }

    static segtree<T> RangeSumQuery(ll len, T e = 0) {
        return segtree<T>(len, e, [](T a, T b) { return a + b; });
    }
};

ll inversion_number(vll &v) {
    int n = v.size();
    if(n == 1) return 0;
    vll v1(v.begin(),v.begin() + n / 2);
    vll v2(v.begin() + n / 2, v.end());
    ll res = inversion_number(v1) + inversion_number(v2);
    int p = 0, q = 0;
    rep(i, n){
        if(q == v2.size() || (p < v1.size() && v1[p] <= v2[q])){
            v[i] = v1[p++];
        }else{
            v[i] = v2[q++];
            res += v1.size() - p;
        }
    }
    return res;
}



int main(){
    ll n; cin >> n;
    vll a(n);
    rep(i, n) cin >> a[i];

    map<ll, vector<ll>> index;
    rep(i, n) index[a[i]].push_back(i);

    segtree<ll> seg = segtree<ll>::RangeSumQuery(n);
    rep(i, n) {
        seg[i] = 1;
    }
    seg.build();

    ll ans = 0;
    for (auto [_, idxs] : index) {
        for (ll i : idxs) {
            seg.update(i, 0);
        }
        for (ll i : idxs) {
            ll l = seg.query(0, i);
            ll r = seg.query(i, n);
            ans += min(l, r);
        }
    }

    cout << ans << "\n";

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 16812
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

template <typename T>
struct BIT {
    int n;
    vector<T> bit;
    BIT(int n_) : n(n_ + 1), bit(n, 0) {}
    void add(int i, T x) {
        for (int idx = i; idx < n; idx += (idx & -idx)) {
            bit[idx] += x;
        }
    }
    T sum(int i) {
        T s(0);
        for (int idx = i; idx > 0; idx -= (idx & -idx)) {
            s += bit[idx];
        }
        return s;
    }
};

#define INF 100003

int main(){
    int n;cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++)cin >> a[i];

    BIT<int> fw(100007);

    map<int,vector<int>> mv;
    for (int i = 0; i < n; i++){
        int k = fw.sum(INF) - fw.sum(a[i]);
        mv[a[i]].push_back(k);
        fw.add(a[i],1);
    }

    ll ans = 0;
    int cnt = 0;
    for (auto [k,v]: mv){
        cnt += v.size();
        for (auto p:v){
            ans += min(p, n-p-cnt);
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 15104
final_rank: 12

code_snippet: #include<bits/stdc++.h>
#define int long long
using namespace std;
int trn[400000], N;
void add(int pos, int val){
	while(pos <= N){
		trn[pos] += val;
		pos += pos & -pos;
	}
}
int get(int pos){
	int rtv = 0;
	while(pos){
		rtv += trn[pos];
		pos -= pos & -pos;
	}
	return rtv;
}
vector<int> alln[400000];
int n, a[400000], ans;
signed main(){
	cin >> n; N = 300000;
	for(int i = 1; i <= n; i++) {cin >> a[i]; alln[a[i]].push_back(i); add(i, 1);}
	sort(a + 1, a + 1 + n);
	n = unique(a + 1, a + 1 + n) - a - 1;
	for(int i = 1; i <= n; i++) {
		int l = 0, r = alln[a[i]].size() - 1;
		while(l <= r){
			int dist_l = min(get(alln[a[i]][l] - 1), get(N) - get(alln[a[i]][l]));
			int dist_r = min(get(alln[a[i]][r] - 1), get(N) - get(alln[a[i]][r]));
			ans += min(dist_l, dist_r);
			if(dist_l < dist_r) add(alln[a[i]][l++], -1);
			else add(alln[a[i]][r--], -1);
		}
	}
	cout << ans << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 19640
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
int trn[400000], N;
void add(int pos, int val){
	while(pos <= N){
		trn[pos] += val;
		pos += pos & -pos;
	}
}
int get(int pos){
	int rtv = 0;
	while(pos){
		rtv += trn[pos];
		pos -= pos & -pos;
	}
	return rtv;
}
vector<int> alln[400000];
int n, a[400000], ans;
int main(){
	cin >> n; N = 300000;
	for(int i = 1; i <= n; i++) {cin >> a[i]; alln[a[i]].push_back(i); add(i, 1);}
	sort(a + 1, a + 1 + n);
	n = unique(a + 1, a + 1 + n) - a - 1;
	for(int i = 1; i <= n; i++) {
		int l = 0, r = alln[a[i]].size() - 1;
		while(l <= r){
			int dist_l = min(get(alln[a[i]][l] - 1), get(N) - get(alln[a[i]][l]));
			int dist_r = min(get(alln[a[i]][r] - 1), get(N) - get(alln[a[i]][r]));
			ans += min(dist_l, dist_r);
			if(dist_l <= dist_r) add(alln[a[i]][l++], -1);
			else add(alln[a[i]][r--], -1);
		}
	}
	cout << ans << '\n';
}
accuracy: 0.8222222222222222
time_ms: 30
memory_kb: 17724
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll maxn=1000001;
vector<ll> pos[maxn];
ll n,m,a[maxn],ans,tr[maxn];
ll lowbit(ll k){
	return k&-k;
}
void add(ll x){
	for(;x<=n;x+=lowbit(x))tr[x]++;
}
ll sum(ll x){
	ll res=0;
	for(;x;x-=lowbit(x))res+=tr[x];
	return res;
}
int main(){
	cin>>n;
	for(ll i=1;i<=n;i++){
		cin>>a[i];
		pos[a[i]].push_back(i);
	}
	m=n;
	for(auto&& p:pos){
		if(p.empty())continue;
		ll l=0,r=(ll)p.size()-1;
		while(l<=r){
			ll lres=p[l]-sum(p[l])-1;
			ll rres=m-p[r]+sum(p[r]);
			if(lres<rres)ans+=lres,add(p[l]),l++;
			else ans+=rres,add(p[r]),r--;
			m--;
//			cout<<lres<<" "<<rres<<" "<<ans<<endl;
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 36108
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define FILE(x) freopen(x".in", "r", stdin);freopen(x".out", "w", stdout);
#define endl '\n'
#define int long long
const int N=1e5+5;
int n,ans,cnt=1;
struct node{
	int num,val;
}a[N];
int id[N];
int f[N][2];
struct T{
	int c[N];
	inline int lowbit(int x){return x&(-x);}
	inline void add(int u,int v){
		while(u<=n){
			c[u]+=v;
			u+=lowbit(u);
		}
	}
	inline int ask(int x){
		int res=0;
		while(x){
			res+=c[x];
			x-=lowbit(x);
		}
		return res;
	}
}t1,t2;
signed main(){
	cin.tie(nullptr)->ios::sync_with_stdio(false);
	cout.tie(nullptr);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].val;
		a[i].num=i;
	}
	stable_sort(a+1,a+n+1,[&](node p1,node p2){
		return p1.val<p2.val;
	});
	id[a[1].num]=1;
	for(int i=2;i<=n;i++){
		if(a[i].val==a[i-1].val) id[a[i].num]=cnt;
		else id[a[i].num]=++cnt;
	}
	for(int i=1;i<=n;i++){
		t1.add(id[i],1);
		f[i][0]=i-t1.ask(id[i]);
	}
	for(int i=n;i>=1;i--){
		t2.add(id[i],1);
		f[i][1]=n-i+1-t2.ask(id[i]);
	}
	for(int i=1;i<=n;i++){
		ans+=min(f[i][1],f[i][0]);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 8828
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define point pair<ll, ll>
#define X first
#define Y second
#define MASK bitset<19>
#define ll long long

const int N = 1e5 + 10;
int fen[2][N];
int a[N];
void add(int idx ,int val, int i){
	while (idx < N) fen[i][idx] += val, idx += (idx & -idx);
}
int get_sum(int idx, int i){
	int sum = 0; 	while (idx > 0)	sum += fen[i][idx], idx -= (idx & -idx); 		return sum;
}


vector<int> indices[N];

int main() {
    cin.tie(0);
    cin.sync_with_stdio(0);

    int n; cin>>n;
    for(int i = 1; i <= n; i++)
    {
        add(i, 1, 0);
        add(i, 1, 1);

        cin>>a[i];
        indices[a[i]].push_back(i);
    }


    ll ans = 0;
    for(int i = 1; i < N; i++){
        int L = 0, R = indices[i].size() - 1;
        while(L <= R)
        {
                int idx1 = indices[i][L];
                int idx2 = indices[i][R];

                int L1 = get_sum(idx1, 0);
                int R1 = get_sum(n - idx1 + 1, 1);

                int L2 = get_sum(idx2, 0);
                int R2 = get_sum(n - idx2 + 1, 1);

                int cost1 = min(L1, R1) - 1;
                int cost2 = min(L2, R2) - 1;

                if(cost1 <= cost2)
                {
                    ans += cost1;
                    add(idx1, -1, 0);
                    add(n - idx1 + 1, -1, 1);
                    L++;
                }else
                {
                    ans += cost2;
                    add(idx2, -1, 0);
                    add(n - idx2 + 1, -1, 1);
                    R--;
                }
        }
    }


    cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 10024
final_rank: 3

code_snippet: #include<iostream>
#define ll long long
using namespace std;
const int maxn = 1e6 + 50;
ll z[maxn]={0};
ll y[maxn]={0};
int n;
ll a[maxn];
ll lowbit(ll x){
	return x&(-x);
}
ll query(ll i,ll t[]){
	ll ans = 0;
	while(i){
		ans += t[i];
		i-=lowbit(i);
	}
	return ans;
}
void add(ll i,ll x,ll t[]){
	while(i < maxn){
		t[i]+=x;
		i+=lowbit(i);
	}
}
ll num1[maxn];
ll num2[maxn];
void sol(){
	for(int i = n-1;i >= 0;--i){
		num1[i] = query(maxn-a[i]-1,y);
		add(maxn-a[i],1,y);
	}
	ll ans = 0;
	for(int i = 0;i < n;++i){
		num2[i] = query(maxn-a[i]-1,z);
		add(maxn - a[i],1,z);
	}
	for(int i = 0;i < n;++i) ans+=min(num1[i],num2[i]);
	cout<<ans<<endl;
}
int main(){
	cin>>n;
	for(int i = 0;i < n;++i) scanf("%lld",&a[i]);
	sol();
}
accuracy: 1
time_ms: 10
memory_kb: 20752
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define lowbit(x) ((x) & (-x))
using namespace std;
const int MAXN = 1e5 + 10;

int n, tree1[MAXN << 2], tree2[MAXN << 2];
int aa[MAXN], bb[MAXN];

void add1(int x, int v)
{
    while (x <= n)
    {
        tree1[x] += v;
        x += lowbit(x);
    }
}

void add2(int x, int v)
{
    while (x <= n)
    {
        tree2[x] += v;
        x += lowbit(x);
    }
}

int getsum1(int x)
{
    int ans = 0;
    while (x >= 1)
    {
        ans += tree1[x];
        x -= lowbit(x);
    }
    return ans;
}

int getsum2(int x)
{
    int ans = 0;
    while (x >= 1)
    {
        ans += tree2[x];
        x -= lowbit(x);
    }
    return ans;
}

struct cmp1
{
    bool operator()(pair<int, int> a, pair<int, int> b)
    {
        if (a.second < b.second)
        {
            return false;
        }
        else if (a.second == b.second)
        {
            if (a.first < b.first)
                return false;
            else
                return true;
        }
        else
        {
            return true;
        }
    }
};

struct cmp2
{
    bool operator()(pair<int, int> a, pair<int, int> b)
    {
        if (a.second < b.second)
        {
            return false;
        }
        else if (a.second == b.second)
        {
            if (a.first > b.first)
                return false;
            else
                return true;
        }
        else
        {
            return true;
        }
    }
};

int main()
{
    cin >> n;
    priority_queue<pair<int, int>, vector<pair<int, int>>, cmp1> q1;
    priority_queue<pair<int, int>, vector<pair<int, int>>, cmp2> q2;
    for (int i = 1; i <= n; i++)
    {
        int in = 0;
        cin >> in;
        add1(i, 1);
        add2(i, 1);
        q1.push(pair<int, int>(i, in));
        q2.push(pair<int, int>(i, in));
    }
    int ans = 0;
    while (q1.size())
    {
        pair<int, int> x = q1.top();
        q1.pop();
        aa[x.first] = getsum1(x.first - 1);
        add1(x.first, -1);
    }
    while (q2.size())
    {
        pair<int, int> x = q2.top();
        q2.pop();
        bb[x.first] = getsum2(n) - getsum2(x.first);
        add2(x.first, -1);
    }
    for (int i = 1; i <= n; i++)
        ans += min(aa[i], bb[i]);
    cout << ans << endl;
    return 0;
}
accuracy: 0.8222222222222222
time_ms: 30
memory_kb: 6304
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define FILE(x) freopen(x".in", "r", stdin);freopen(x".out", "w", stdout);
#define endl '\n'
#define ll long long
const int N=1e5+5;
int n,ans,cnt=1;
struct node{
	int num,val;
}a[N];
int id[N];
int f[N][2];
struct T{
	int c[N];
	inline int lowbit(int x){return x&(-x);}
	inline void add(int u,int v){
		while(u<=n){
			c[u]+=v;
			u+=lowbit(u);
		}
	}
	inline int ask(int x){
		int res=0;
		while(x){
			res+=c[x];
			x-=lowbit(x);
		}
		return res;
	}
}t1,t2;
signed main(){
	cin.tie(nullptr)->ios::sync_with_stdio(false);
	cout.tie(nullptr);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].val;
		a[i].num=i;
	}
	stable_sort(a+1,a+n+1,[&](node p1,node p2){
		return p1.val<p2.val;
	});
	id[a[1].num]=1;
	for(int i=2;i<=n;i++){
		if(a[i].val==a[i-1].val) id[a[i].num]=cnt;
		else id[a[i].num]=++cnt;
	}
	for(int i=1;i<=n;i++){
		t1.add(id[i],1);
		f[i][0]=i-t1.ask(id[i]);
	}
	for(int i=n;i>=1;i--){
		t2.add(id[i],1);
		f[i][1]=n-i+1-t2.ask(id[i]);
	}
	for(int i=1;i<=n;i++){
		ans+=min(f[i][1],f[i][0]);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 0.8222222222222222
time_ms: 10
memory_kb: 6076
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n" 
const ll maxn=1e5+7;
int n;
int a[maxn];
int C[maxn];
int l[maxn];
int r[maxn];
int lowbit(int x){
    return x & (-x);
}

void add(int x,int v){
    while(x<maxn){
        C[x] += v;
        x += lowbit(x);
    }
    return;
}

int Union(int x){
    int sum = 0;
    while(x){
        sum += C[x];
        x -= lowbit(x);
    }
    return sum;
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n;i++){
        scanf("%d", &a[i]);
        l[i] = Union(maxn - 1) - Union(a[i]);
        add(a[i], 1);
    }
    memset(C, 0, sizeof(C));
    for (int i = n; i >= 1;i--){
        r[i] = Union(maxn - 1) - Union(a[i]);
        add(a[i], 1);
    }
    int ans = 0;
    for (int i = 1; i <= n;i++){
        ans += min(l[i], r[i]);
    }
    cout << ans << endl;
    return 0;
}
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//fclose(stdin);
//fclose(stdout);
accuracy: 0.8222222222222222
time_ms: 10
memory_kb: 5032
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
struct que{
	int z,p;
}a[maxn];
int n;
bool cmp(const que &a,const que &b){
	if(a.z!=b.z)return a.z<b.z;
	else return a.p<b.p;
}
int pos[maxn];
struct T{
	int l,r,z;
}tree[maxn*4];
void build(int l,int r,int p){
	tree[p].l=l; tree[p].r=r; tree[p].z=0;
	if(l==r){
		pos[l]=p;
		return;
	}
	build(l,(l+r)/2,p*2);
	build((l+r)/2+1,r,p*2+1);
}
int query(int x){
	int i=pos[x],ans=0;
	while(i){
		ans+=tree[i].z;
		i=i/2;
	}
	return ans;
}
long long ans=0;
void updata(int l,int r,int p,int x){
//	cout<<l<<" "<<r<<endl;
	if(l>r) return;
	if(tree[p].l==l&&tree[p].r==r){
		tree[p].z+=x;
		return;
	}
	if(l>=tree[p*2+1].l){
		updata(l,r,p*2+1,x);
		return;
	}
	if(r<=tree[p*2].r){
		updata(l,r,p*2,x);
		return;
	}
	updata(l,tree[p*2].r,p*2,x);
	updata(tree[p*2+1].l,r,p*2+1,x);
}
int getp(int x){
	return a[x].p+query(a[x].p);
}
int main(){
	cin>>n;
	build(1,n,1);
	for(int i=1;i<=n;i++) scanf("%d",&a[i].z);
	for(int i=1;i<=n;i++) a[i].p=i;
	sort(a+1,a+n+1,cmp);
	int l=1,r=n;
	for(int i=1,j,k;i<=n;i=j){
		for(j=i;a[j].z==a[i].z&&j<=n;j++);
		int k1=i,k2=j-1;
		while(k1<=k2){
			int p1=getp(k1),p2=getp(k2);
			if(p1-l<=r-p2){
				updata(1,p1-1,1,1);
				ans+=p1-l;
				l++;
				k1++;
			}
			else{
				updata(p2+1,n,1,-1);
				ans+=r-p2;
				r--;
				k2--;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 0.8222222222222222
time_ms: 20
memory_kb: 8736
final_rank: 18

code_snippet: #include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#define lowbit(x) (x&(-x))
#define int long long
const int N=1e5+1;

int n;
std::set<int> s[N];
std::pair<int, int> a[N];

int tree[N];

void update(int x, int d) {
    while(x<=n) {
        tree[x]+=d;
        x+=lowbit(x);
    }
}

int query(int x) {
    int res=0;
    while(x) {
        res+=tree[x];
        x-=lowbit(x);
    }
    return res;
}

signed main() {
    scanf("%lld", &n);
    
    for(int i=1; i<=n; i++) {
        int x;
        scanf("%lld", &x);
        s[x].insert(i);
    }
    int pos=1;

    int ans=0;
    for(int i=1; i<=n; i++) {
        while(!s[pos].size()) {
            pos++;
        }
        int sum1=query(*s[pos].begin()), sum2=query(*s[pos].rbegin());

        if(*s[pos].begin()-sum1-1<=n-i+1+sum2-*s[pos].rbegin()) {
            ans+=*s[pos].begin()-sum1-1;
            update(*s[pos].begin(), 1);
            s[pos].erase(s[pos].begin());
        }
        else {
            ans+=n-i+1+sum2-*s[pos].rbegin();
            update(*s[pos].rbegin(), 1);
            s[pos].erase(--s[pos].end());
        }
    }
    printf("%lld\n", ans);
}
accuracy: 1
time_ms: 20
memory_kb: 13492
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(a) begin(a), end(a);

struct BIT{
	vector<ll> a;
	BIT(ll n) : a(n + 1) {};
	void add(ll i, ll x){
		i++;
		while(i < a.size()) a[i] += x, i += i & -i;
	}
	ll sum(ll r){
		ll s = 0;
		while(r) s += a[r], r -= r & -r;
		return s;
	}
	ll sum(ll l,ll r){return sum(r) - sum(l);}
};

int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	for(int i = 0;i < n;i++) cin >> a[i],a[i]--;;
	BIT bit(n);
	for(int i = 0;i < n;i++) bit.add(i,1);
	vector<vector<int>> idx(1e5);
	for(int i = 0;i < n;i++) idx[a[i]].push_back(i);
	ll ans = 0;
	for(int i = 0;i < idx.size();i++){
		for(int j = 0;j < idx[i].size();j++) bit.add(idx[i][j],-1);
		ll cur = 0;
		for(int j = 0;j < idx[i].size();j++){
			cur += bit.sum(idx[i][j] + 1,n);
		}
		ll mn = cur;
		for(int j = 0;j < idx[i].size();j++){
			cur -= bit.sum(idx[i][j] + 1,n);
			cur += bit.sum(0,idx[i][j]);
			mn = min(mn,cur);
		}
		ans += mn;
		
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 9996
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

/*
	考察
	小さい順に左か右かによける
	自分より左・右にある自分より大きい値の個数がわかれば良い
*/

struct ST{
	int n;
	vector<ll> dat;
	ST(int _n): n(_n), dat(_n*2) {}
	void add(int i, ll x){
		i += n;
		dat[i] += x;
		while(i){
			i /= 2;
			dat[i] = dat[i*2]+dat[i*2+1];
		}
	}
	ll sum(int l, int r){
		l += n, r += n;
		ll ret = 0;
		while(l < r){
			if(l & 1) ret += dat[l++];
			if(r & 1) ret += dat[--r];
			l /= 2, r /= 2;
		}
		return ret;
	}
};

int main(){
	int N; cin >> N;
	vector<int> A(N);
	for(int i = 0; i < N; i++) cin >> A[i];

	const int mx = 2e5;
	ST st1(mx), st2(mx);
	vector<ll> larger(N, 1<<30);
	for(int i = 0; i < N; i++){
		larger[i] = min(larger[i], st1.sum(A[i]+1, mx));
		st1.add(A[i], 1);
	}
	for(int i = N-1; i >= 0; i--){
		larger[i] = min(larger[i], st2.sum(A[i]+1, mx));
		st2.add(A[i], 1);
	}

	ll ans = 0;
	for(int i = 0; i < N; i++) ans += larger[i];
	cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 10436
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << x << endl;

struct BIT {
    int n;
    vector<ll> d;
    BIT(int n) : n(n), d(n+1) {}

    void add(int idx, ll val) {
        ++idx;
        for (int i = idx; i <= n; i += i & -i) {
            d[i] += val;
        }
    }
    ll sum(int idx) {
        ll res = 0;
        for (int i = idx; i > 0; i -= i & -i) {
            res += d[i];
        }
        return res;
    }
    ll sum(int l, int r) {
        return sum(r) - sum(l);
    }
};

int main() {
    int n;
    cin >> n;

    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    // vector<pair<int,int>> v(n);
    // for (int i = 0; i < n; i++) v[i] = {a[i],i};
    // sort(ALL(v));
    vector<vector<int>> v(1e5+1);
    for (int i = 0; i < n; i++) {
        v[a[i]].emplace_back(i);
    }

    BIT b(n);
    for (int i = 0; i < n; i++) {
        b.add(i,1);
    }

    ll ans = 0;
    for (int t = 0; t <= 1e5; ++t) {
        int l = 0, r = v[t].size()-1;
        while (l <= r) {
            int li = b.sum(0,v[t][l]);
            int ri = b.sum(v[t][r]+1,n);

            if (li <= ri) {
                ans += li;
                b.add(v[t][l],-1);
                ++l;
            }
            else {
                ans += ri;
                b.add(v[t][r],-1);
                --r;
            }
        }
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 9636
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>

struct Fenwick {
    int n;
    std::vector<int> data;
    Fenwick(int N) : n{N}, data(n + 1) {}
    int lsb(int x) const {
        return x & (-x);
    }
    void add(int i, int x) {
        assert(0 <= i and i < n);
        for ( i++ ; i < (int)data.size() ; i += lsb(i)) {
            data[i] += x;
        }
    }
    int pref(int r) const {
        assert(0 <= r and r <= n);
        int res{};
        for ( ; r ; r -= lsb(r)) res += data[r];
        return res;
    }
    int prod(int l, int r) const {
        return -pref(l) + pref(r);
    }
};

const int MAX{100010};
std::vector<int> pos[MAX];

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int N;
    std::cin >> N;
    std::vector<int> A(N);
    for (auto& a : A) std::cin >> a;
    for (int i{} ; i < N ; i++) pos[A[i]].push_back(i);
    Fenwick fen(N);
    for (int i{} ; i < N ; i++) fen.add(i, 1);
    long long ans{};
    for (int i{} ; i < MAX ; i++) {
        for (auto j : pos[i]) fen.add(j, -1);
        for (auto j : pos[i]) {
            ans += std::min(fen.pref(j), fen.prod(j, N));
        }
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 9272
final_rank: 2