# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
559887 | cegax | Arranging Shoes (IOI19_shoes) | C++17 | 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(c) (c).begin(), (c).end()
#define rall(A) A.rbegin(),A.rend()
#define pb push_back
#define dbg(x) do {cerr << #x <<" = " << (x) << endl; } while (false)
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
//cout << setprecision(12) << fixed;
struct BIT {
vector<int> bit; // binary indexed tree
int n;
BIT(int n) {
this->n = n;
bit.assign(n, 0);
}
BIT(vector<int> a) : BIT(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n; cin >> n;
ll ans = 0;
vi L[n+1], R[n+1];
int s[2 * n];
for(int i = 0; i < 2 * n; i++) {
cin >> s[i];
if(s[i] < 0)
L[-s[i]].pb(i+1);
else
R[s[i]].pb(i+1);
}
int a[2*n + 1];
bool vis[2 * n + 1];
memset(vis, 0, sizeof(vis));
int ind = 1;
for(int i = 2 * n - 1; i >= 0; i--) {
if(vis[i]) continue;
int l, r;
l = L[abs(s[i])].back();
r = R[abs(s[i])].back();
vis[l-1] = vis[r-1] = 1;
L[abs(s[i])].pop_back();
R[abs(s[i])].pop_back();
if(l > r) ans++;
a[l] = a[r] = ind;
ans += abs(r-l)-1;
ind++;
}
cout << ans << "\n";
return 0;
}