Submission #487116

#TimeUsernameProblemLanguageResultExecution timeMemory
487116Spade1Arranging Shoes (IOI19_shoes)C++17
100 / 100
61 ms13552 KiB
#include <bits/stdc++.h> #include "shoes.h" #define ll long long #define pii pair<int, int> #define st first #define nd second #define pb push_back using namespace std; int fw[200020]; vector<pii> pos[200020]; vector<pii> swp; void update(int i, int val) { for (; i <= 200000; i += i&-i) { fw[i] += val; } } int query(int i) { int cnt = 0; for (; i > 0; i -= i&-i) { cnt += fw[i]; } return cnt; } ll count_swaps(vector<int> s) { int n = s.size()/2; s.insert(s.begin(), 0); ll ans = 0; for (int i = 1; i <= 2*n; ++i) { update(i, 1); pos[abs(s[i])].pb({s[i], i}); } for (int i = 1; i <= n; ++i) { sort(pos[i].begin(), pos[i].end()); int m = pos[i].size(); for (int j = 0; j < m/2; ++j) { int l = pos[i][j].nd; int r = pos[i][j + m/2].nd; if (l > r) { ++ans; swap(l, r); } swp.pb({l, r}); } } sort(swp.begin(), swp.end()); for (auto [l, r] : swp) { ans += query(r - 1) - query(l); update(r, -1); update(l, -1); } 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...