This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
struct fw {
int n;
vector <int> t;
fw(int size): n(size), t(n + 1) {}
void update(int ind, int x) {
ind++;
while(ind <= n) {
t[ind] += x;
ind += ind & (-ind);
}
}
int query(int ind) {
int ans = 0;
ind++;
while(ind) {
ans += t[ind];
ind -= ind & (-ind);
}
return ans;
}
};
long long count_swaps(std::vector<int> s) {
int n = s.size();
return (n / 2) * (n / 2 - 1) / 2;
fw t(n);
map <int, queue<int> > mp;
int ans = 0;
for(int i = 0; i < n; i++) {
if(!mp[-s[i]].empty()) {
int ind = mp[-s[i]].front();
t.update(ind + 1, 1);
t.update(i, -1);
ind += t.query(ind);
// cout << i << " " << ind << "\n";
mp[-s[i]].pop();
ans += (i - ind) - (s[i] > 0);
}
else {
mp[s[i]].push(i);
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |