Submission #171623

#TimeUsernameProblemLanguageResultExecution timeMemory
171623losmi247Arranging Shoes (IOI19_shoes)C++14
10 / 100
56 ms25564 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5+45; ll n,a[N]; ll drvo[10*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; ll id = 1; for(int t = 1; t <= n/2; t++){ ll poz = *s[-a[id]+n].begin(); s[a[id]+n].erase(id); s[-a[id]+n].erase(poz); sol += get(1,n,id,poz,1)-2; if(a[id] > 0){ sol++; } //cout << id << " " << poz << " " << sol << endl; update(1,n,id,1); update(1,n,poz,1); if(poz == id+1){ id += 2; } else{ id++; } } return sol; } /*int main(){ vector <int> v = {-2, 2, 2, -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...