Submission #718864

#TimeUsernameProblemLanguageResultExecution timeMemory
718864Yell0Arranging Shoes (IOI19_shoes)C++17
100 / 100
74 ms15176 KiB
#include <bits/stdc++.h> using namespace std; const int MN=1e5+2; typedef long long ll; struct bit { vector<int> arr; bit(int sz) { arr=vector<int>(sz,0); } void upd(int i,int v) { for(;i<(int)arr.size();i+=-i&i) arr[i]+=v; } int qry(int i) { int ret=0; for(;i>0;i-=-i&i) ret+=arr[i]; return ret; } }; ll count_swaps(vector<int> S) { ll N=S.size()/2,ans=0,sub=0; bit bit(N*2+2); vector<int> sti(N*2+2,0),edi(N*2+2,0); S.insert(S.begin(),0); vector<vector<int>> pos(2*MN); for(int i=1;i<=2*N;++i) pos[S[i]+N].push_back(i); for(int i=0;i<N;++i) for(int j=0;j<(int)pos[i].size();++j) { ans+=(pos[N+N-i][j]<pos[i][j]); int f=min(pos[N+N-i][j],pos[i][j]),s=max(pos[N+N-i][j],pos[i][j]); sti[s]=f; edi[f]=s; } for(int i=1;i<=2*N;++i) { if(sti[i]) { bit.upd(sti[i],-2); if(sti[i]==i-1) { bit.upd(sti[i],1); continue; } ans+=i-sti[i]-1; sub+=bit.qry(i)-bit.qry(sti[i]); bit.upd(i,1); } else bit.upd(i,1); } return ans-sub/2; }
#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...