# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
808811 | OrazB | 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>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long int
#define pii pair <int, int>
#define pb push_back
#define ff first
#define ss second
const int N = 2e5+5;
int vis[N];
deque<int> lft[N], rgt[N];
ll count_swaps(vector<int> S){
int n = S.size();
S.insert(S.begin(), -1);
for (int i = 1; i <= n; i++){
if (S[i] > 0) rgt[S[i]].pb(i);
else lft[-S[i]].pb(i);
}
ll ans = 0;
set<int> s;
for (int i = 1; i <= n; i++){
while(s.size() and (*s.begin()) < i) s.erase(s.begin());
if (vis[i]) continue;
int j;
if (S[i] > 0) j = lft[S[i]].front();
else j = rgt[-S[i]].front();
lft[abs(S[i])].pop_front();
rgt[abs(S[i])].pop_front();
ans += (j-i-s.size()-(S[i] < 0));
s.insert(j);
vis[j] = 1;
}
return ans;
}
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> S(2*n);
for (int i = 0; i < 2*n; i++) cin >> S[i];
cout << count_swaps(S);
}