# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
810716 | OAleksa | 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>
#include "shoes.h"
#define f first
#define s second
using namespace std;
const int maxn = 1e5 + 69;
set<int> l[maxn], r[maxn];
long long count_swaps(vector<int> s) {
int n = s.size();
vector<int> a = s;
for(int i = 0;i < n;i++) {
if(a[i] < 0)
l[abs(a[i])].insert(i);
else
r[a[i]].insert(i);
}
long long ans = 0;
for(int i = 0;i < n;i += 2) {
if(a[i] < 0) {
auto u = *r[abs(a[i])].begin();
r[abs(a[i])].erase(u);
for(int j = u - 1;j > i;j--) {
++ans;
if(a[j] < 0) {
l[abs(a[j])].erase(j);
l[abs(a[j])].insert(j + 1);
}
else {
r[a[j]].erase(j);
r[a[j]].insert(j + 1);
}
swap(a[j], a[j + 1]);
}
l[abs(a[i])].erase(i);
}
else {
auto u = *l[a[i]].begin();
l[abs(a[j])].erase(u);
for(int j = u - 1;j >= i;j--) {
++ans;
if(a[j] < 0) {
l[abs(a[j])].erase(j);
l[abs(a[j])].insert(j + 1);
}
else {
r[a[j]].erase(j);
r[a[j]].insert(j + 1);
}
swap(a[j], a[j + 1]);
}
}
}
return ans;
}
// int main()
// {
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
// int tt = 1;
// //cin >> tt;
// while(tt--) {
// int n;
// cin >> n;
// vector<int> a(2 * n);
// for(int i = 0;i < 2 * n;i++)
// cin >> a[i];
// cout << count_swaps(a) << endl;
// }
// return 0;
// }