# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
805599 | Meloric | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
//#define int int64_t
#define pii pair<int, int>
#define X first
#define Y second
#define all(x) (x).begin(),(x).end()
#define lb lower_bound
#define ub upper_bound
using namespace std;
const int inf = 1e18;
void p(auto A){
for(auto e : A)cout << e << ' ';
cout << '\n';
}
int64_t count_swaps(vector<int> S){
int n = S.size();
vector<int>nxt(n, -1);
map<int, vector<int>> mp;
for(int i = 0; i< n; i++)mp[S[i]].pb(i);
for(auto[k, v]:mp)if(k<0){
vector<int>tmp = mp[-k];
for(int i = 0; i< tmp.size(); i++){
nxt[min(tmp[i], v[i])]=max(tmp[i], v[i]);
}
}
vector<int>ft(n+5);
auto upd = [&](int pos, int val){
pos++;
for(; pos <= n; pos+=pos&(-pos))ft[pos]+=val;
};
auto qer = [&](int pos){
pos++;
int ret = 0;
for(;pos>0; pos-=pos&(-pos))ret+=ft[pos];
return ret;
};
for(int i = 0; i< n; i++)upd(i, 1);
vector<int> used(n);
int64_t ans = 0;
for(int i = 0; i< n; i++){
if(used[i])continue;
if(S[i]<0)upd(i, -1);
ans+=qer(nxt[i]-1);
if(S[i]>0)upd(i, -1);
upd(nxt[i], -1);
used[i]=1;
used[nxt[i]]=1;
}
return ans;
}
void solve(){
int n; cin >> n;
vector<int>A(2*n);
for(int i = 0; i< 2*n; i++)cin >> A[i];
cout << count_swaps(A);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin >> t;
while(t--)solve();
}