Submission #766697

#TimeUsernameProblemLanguageResultExecution timeMemory
766697t6twotwoArranging Shoes (IOI19_shoes)C++17
100 / 100
151 ms139096 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; using ll = long long; ll count_swaps(vector<int> s) { int n = s.size() / 2; vector<int> bit(2 * n + 1); auto add = [&](int p, int v) { for (int i = p + 1; i <= 2 * n; i += i & -i) { bit[i] += v; } }; auto sum = [&](int p) { int ans = 0; for (int i = p; i > 0; i -= i & -i) { ans += bit[i]; } return ans; }; ll ans = 0; queue<int> a[n + 1][2]; for (int i = 0; i < 2 * n; i++) { int q = s[i] > 0; if (s[i] < 0) { s[i] = -s[i]; } if (!a[s[i]][!q].empty()) { int p = a[s[i]][!q].front(); a[s[i]][!q].pop(); ans += (i - p - 1) - (sum(i) - sum(p)) + !q; add(i, 1); add(p, -1); } else { a[s[i]][q].push(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...