| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 155976 | GioChkhaidze | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
using namespace std;
const int N=100005;
int a[2*N],G[2*N],f1[2*N],f2[2*N];
void Upd(int x,int dl) {
while (dl<=2*N) {
G[x]+=dl;
x+=(x & -x);
}
}
void Get(int x) {
int res=0;
while (0<dl) {
res+=G[x];
x-=(x & -x);
}
return res;
}
long long count_swaps(std::vector<int> s) {
long long ans=0;
for (int i=0; i<s.size(); i++) {
a[i+1]=s[i];
if (a[i+1]>0) f1[a[i+1]]=i+1;
if (a[i+1]<0) f2[-a[i+1]]=i+1;
Upd(i+1,1);
}
for (int i=1; i<=s.size()/2; i++) {
int l=1,r=2*(s.size()),mid,idx=-1;
while (l<=r) {
mid=(l+r)/2;
if (1<=Get(mid)) { idx=mid; r=mid-1; }
else l=mid+1;
}
if (a[idx]<0) {
int x=-a[idx];
ans+=Get(f1[x]-1)-1;
Upd(idx,-1);
Upd(f1[x],-1);
}
else {
int x=a[idx];
ans+=Get(f2[x]-1);
Upd(idx,-1);
Upd(f2[x],-1);
}
}
return ans;
}
