Submission #1021577

#TimeUsernameProblemLanguageResultExecution timeMemory
1021577KasymKArranging Shoes (IOI19_shoes)C++17
100 / 100
111 ms28316 KiB
#include "bits/stdc++.h" using namespace std; #define pb push_back #define ll long long #define ff first #define ss second #define all(v) v.begin(), v.end() const int N = 3e5+5; set<int> S[N]; int bel[N], en[N], st[N], cnt[N], vis[N]; ll count_swaps(vector<int> v){ int n = (int)v.size(); for(int i = 0; i < n; ++i) S[v[i]+n].insert(i); int ad = sqrt(n); int nire = -1; for(int i = 0; i < n; ++i){ int pk = i/ad; bel[i] = pk, cnt[pk]++, en[pk] = i; if(pk > nire) st[pk] = i; nire = pk; } ll ans = 0; for(int i = 0; i < n-1; i++){ if(vis[i]) continue; if(v[i] > 0) ans++; auto j_ = S[-v[i]+n].upper_bound(i); S[-v[i]+n].erase(j_); int j = *j_; vis[j] = 1, cnt[bel[j]]--; int l = i+1, r = j-1; if(bel[l] == bel[r] or l > r){ for(int k = l; k <= r; ++k) ans += (!vis[k]); continue; } for(int k = l; k <= en[bel[l]]; ++k) ans += (!vis[k]); for(int k = st[bel[r]]; k <= r; ++k) ans += (!vis[k]); for(int k = bel[l]+1; k < bel[r]; ++k) ans += (cnt[k]); } return ans; }
#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...