Submission #844033

#TimeUsernameProblemLanguageResultExecution timeMemory
844033Mr_HusanboyArranging Shoes (IOI19_shoes)C++17
100 / 100
61 ms15788 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; template <typename T> int len(T &a){return a.size();} struct fenwick{ vector<int> t; int n; fenwick(int _n){ n = _n; t.assign(n, 0); } void inc(int i, int val = 1){ for(; i < n; i |= i + 1) t[i] += val; } int sum(int i){ int s = 0; for(; i >= 0; i = (i & (i + 1)) - 1) s += t[i]; return s; } int get(int l, int r){ return sum(r) - sum(l - 1); } }; #define ll long long long long count_swaps(vector<int> s){ int n = len(s) / 2; vector<vector<vector<int>>> g(2, vector<vector<int>> (n + 1)); for(int i = 2 * n - 1; i >= 0; i --){ g[s[i]<0][abs(s[i])].push_back(i); } vector<int> done(2 * n); fenwick t(2 * n); ll ans = 0; for(int i = 0; i < 2 * n; i ++){ if(done[i]){ continue; } g[s[i] < 0][abs(s[i])].pop_back(); ans += g[s[i] > 0][abs(s[i])].back() - i + (s[i] > 0) - 1 - t.get(i, g[s[i] > 0][abs(s[i])].back()); t.inc(g[s[i] > 0][abs(s[i])].back()); done[g[s[i] > 0][abs(s[i])].back()] = 1; g[s[i] > 0][abs(s[i])].pop_back(); //cout << ans << endl; } 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...