# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
560089 | PoonYaPat | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n,ans,fen[200001],a[200001];
queue<int> pos[200001];
bool vis[200001];
int find(int x) {
int sum=0;
while (x) {
sum+=fen[x];
x-=(int)(x&-x);
}
return sum;
}
void update(int x, int y) {
while (x<=2*n) {
fen[x]+=y;
x+=(x&-x);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=1; i<=2*n; ++i) {
cin>>a[i];
if (a[i]<0) a[i]=n-a[i];
pos[a[i]].push(i);
}
for (int i=1; i<=2*n; ++i) {
if (vis[i]) continue;
int k=a[i];
if (k<=n) { //right shoes, have to move left shoes
while (vis[pos[k+n].front()]) pos[k+n].pop();
int p=pos[k+n].front();
pos[k+n].pop();
ans+=(p+find(p)-1);
update(i,-1);
update(p,-1);
vis[i]=true;
vis[p]=true;
} else { //left shoes, have to move right shoes
while (vis[pos[k-n].front()]) pos[k-n].pop();
int p=pos[k-n].front();
pos[k-n].pop();
ans+=(p+find(p)-2);
update(i,-1);
update(p,-1);
vis[i]=true;
vis[p]=true;
}
}
cout<<ans;
}