| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1327830 | Mansurt | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#define ll int64_t
using namespace std;
const ll N = 1e5 + 5;
vector<ll> point(N, 1);
int main() {
ll n; cin >> n;
vector<ll> a(n+1);
vector<ll> neg[N], pos[N];
vector<bool> was(n+1, 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] > 0) pos[a[i]].push_back(i);
else neg[(-1)*a[i]].push_back(i);
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (!was[i]) continue;
if (a[i] > 0) {
while (point[a[i]] < pos[a[i]].size() && !was[pos[a[i]][point[a[i]]]]) point[a[i]]++;
ll j = pos[a[i]][point[a[i]]];
ans += abs(i - j);
was[i] = false;
was[j] = false;
point[a[i]]++;
}
else {
ll sz = -a[i];
while (point[sz] < neg[sz].size() && !was[neg[sz][point[sz]]]) point[sz]++;
ll j = neg[sz][point[sz]];
ans += abs(i - j) - 1;
was[i] = false;
was[j] = false;
point[sz]++;
}
}
cout << ans;
}
