Submission #883339

#TimeUsernameProblemLanguageResultExecution timeMemory
883339AkibAzmainArranging Shoes (IOI19_shoes)C++17
100 / 100
492 ms148076 KiB
#include <bits/stdc++.h> using namespace std; #include "shoes.h" long long count_swaps(std::vector<int> a) { int n = a.size (); map < int, deque < int > > c; for (int i = 0; i < n; ++i) c[a[i]].push_back (i); vector < int > st (n); for (int i = 0; i < n; ++i) st[i] = (i + 1) & -(i + 1); auto inc = [&] (int i, int x) { while (i < n) { st[i] += x; i += (i + 1) & -(i + 1); } }; auto sum = [&] (int i) { int ans = 0; while (i >= 0) { ans += st[i]; i -= (i + 1) & -(i + 1); } return ans; }; vector < bool > b (n); long long ans = 0; for (int i = 0; i < n; ++i) { if (b[i]) continue; int j = c[-a[i]].front (); c[-a[i]].pop_front (); c[a[i]].pop_front (); ans += sum (j) - sum (i) - 1; if (a[i] > 0) ++ans; inc (i, 1); inc (j, -1); b[j] = true; } 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...