Submission #145541

#TimeUsernameProblemLanguageResultExecution timeMemory
145541Sarah_MokhtarArranging Shoes (IOI19_shoes)C++14
100 / 100
449 ms26012 KiB
#include "shoes.h" #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+10,M=(N<<4),OO=0x3f3f3f; int pos[N],BIT[M],n; void update(int idx,int val){ ++idx; while(idx<=n){ BIT[idx]+=val; idx+=(idx&-idx); } } ll query(int r){ ll res=0; ++r; while(r>0){ res+=BIT[r]; r-=(r&-r); } return res; } ll count_swaps(vector<int>s){ map<int,vector<int>>m; ll ret=0; n=s.size(); for(int i=0;i<n;++i) update(i,1); for(int i=n-1;i>=0;--i) m[s[i]].push_back(i); for(int i=0;i<n;++i){ if(query(i)-query(i-1)!=1) continue; int k=m[-s[i]].back(); m[s[i]].pop_back(); m[-s[i]].pop_back(); update(i,-1); update(k,-1); ret+=query(k)-query(i); if(s[i]>0) ++ret; } return ret; }
#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...