제출 #213079

#제출 시각아이디문제언어결과실행 시간메모리
213079iris2617Arranging Shoes (IOI19_shoes)C++14
100 / 100
268 ms277500 KiB
#include "shoes.h" #include <queue> #define int long long using namespace std; int arr[200010],n; queue<int> q[200010],q2[200010]; int bit[200010]; bool used[200010]; void build() { int i; for(i=1;i<=n;i++) { if(i+(i&(-i))<=n) { bit[i+(i&(-i))]+=bit[i]; } } } void add(int a,int k) { int i; for(i=a;i<=n;i+=(i&(-i))) { bit[i]+=k; } } int sum(int a) { int i,res=0; for(i=a;i;i-=(i&(-i))) { res+=bit[i]; } return res; } long long count_swaps(std::vector<signed> s) { int i,ans,a; ans=0; n=s.size(); for(i=1;i<=n;i++) { a=s[i-1]; if(a<0) { q[-a].push(i); } else { q2[a].push(i); } arr[i]=a; bit[i]=1; } build(); for(i=1;i<=n;i++) { if(used[i]) { continue; } used[i]=1; if(arr[i]>0) { add(q[arr[i]].front(),-1); add(i,-1); ans+=sum(q[arr[i]].front())+1; used[q[arr[i]].front()]=1; } else { arr[i]*=-1; add(q2[arr[i]].front(),-1); add(i,-1); ans+=sum(q2[arr[i]].front()); used[q2[arr[i]].front()]=1; } q[arr[i]].pop(); q2[arr[i]].pop(); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...