Submission #584418

#TimeUsernameProblemLanguageResultExecution timeMemory
584418KrisjanisPArranging Shoes (IOI19_shoes)C++14
100 / 100
518 ms148020 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pii; struct FenwickTree { vector<int> bit; // binary indexed tree int n; FenwickTree(int n) { this->n = n; bit.assign(n, 0); } int sum(int r) { int ret = 0; for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; return ret; } int sum(int l, int r) { return sum(r) - sum(l - 1); } void add(int idx, int delta) { for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; } }; ll count_swaps(vector<int> s) { ll n = s.size(); map<ll,queue<ll>> m; for(ll i=0;i<n;i++) m[s[i]].push(i); FenwickTree ft(n+100); set<ll> erased; ll res = 0; for(ll i=0;i<n;i++) { if(ft.sum(i,i)==1) continue; m[s[i]].pop(); ll j = m[-s[i]].front(); m[-s[i]].pop(); res += j-i-1; if(s[i]>0) res++; res -= ft.sum(i+1,j); ft.add(i,1); ft.add(j,1); } return res; }
#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...