Submission #171627

#TimeUsernameProblemLanguageResultExecution timeMemory
171627losmi247Arranging Shoes (IOI19_shoes)C++14
100 / 100
254 ms37420 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+45; ll n,a[N]; ll drvo[10*N]; bool gotov[2*N]; void build(int i,int j,int node){ if(i == j){ drvo[node] = 1; return; } int mid = i+(j-i)/2; build(i,mid,2*node); build(mid+1,j,2*node+1); drvo[node] = drvo[2*node]+drvo[2*node+1]; } void update(int i,int j,int pos,int node){ if(i == j){ drvo[node] = 0; return; } int mid = i+(j-i)/2; if(pos <= mid){ update(i,mid,pos,2*node); } else{ update(mid+1,j,pos,2*node+1); } drvo[node] = drvo[2*node]+drvo[2*node+1]; } ll get(int i,int j,int l,int r,int node){ if(j < l || i > r){ return 0; } if(l <= i && r >= j){ return drvo[node]; } int mid = i+(j-i)/2; return get(i,mid,l,r,2*node)+get(mid+1,j,l,r,2*node+1); } set <int> s[2*N]; ll count_swaps(vector <int> v){ n = v.size(); for(int i = 1; i <= n; i++){ a[i] = v[i-1]; } for(int i = 1; i <= n; i++){ s[a[i]+n].insert(i); } build(1,n,1); ll sol = 0; for(int i = 1; i <= n; i++){ if(gotov[i]){ continue; } ll poz = *s[-a[i]+n].begin(); s[a[i]+n].erase(i); s[-a[i]+n].erase(poz); sol += get(1,n,i,poz,1)-2; if(a[i] > 0){ sol++; } //cout << i << " " << poz << " " << sol << endl; update(1,n,i,1); update(1,n,poz,1); gotov[i] = gotov[poz] = 1; } return sol; } /*int main(){ vector <int> v = {2, 1, -1, -2}; cout << count_swaps(v) << endl; }*/
#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...