Submission #528765

#TimeUsernameProblemLanguageResultExecution timeMemory
528765ahmeterenArranging Shoes (IOI19_shoes)C++17
50 / 100
149 ms63640 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5 + 5; ll bit[N]; void update(int ind, int val) { for(; ind < N; ind = (ind | (ind + 1))) bit[ind] += val; } ll query(int ind) { ll res = 0; for(; ind >= 0; ind = (ind & (ind + 1)) - 1) res += bit[ind]; return res; } long long count_swaps(std::vector<int> s) { int n = s.size(); map<int, set<int>> mp; vector<bool> vec(n); for(int i = 0; i < n; i++) { mp[s[i]].insert(i); } for(int i = 0; i < n; i++) update(i, 1); ll ans = 0; for(int i = 0; i < n; i++) { if(vec[i]) continue; ans += (s[i] > 0); auto it = mp[-s[i]].lower_bound(i); int j = *it; mp[-s[i]].erase(it); vec[i] = vec[j] = true; update(i, -1); update(j, -1); ans += query(j) - query(i); } 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...