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>
#define sz(x) ((int) (x).size())
typedef long long ll;
using namespace std;
int n, f[200100];
vector<int> p[200100];
void change(int i, int d) {
for (i++; i <= 2 * n; i += i & -i)
f[i] += d;
}
int sum(int i) {
int s = 0;
for (i++; i; i -= i & -i)
s += f[i];
return s;
}
ll count_swaps(vector<int> s) {
n = sz(s) / 2;
for (int i = 2 * n - 1; i >= 0; i--) {
change(i, 1);
s[i] = s[i] > 0 ? s[i] - 1 : -s[i] - 1 + n;
p[s[i]].push_back(i);
}
ll r = 0;
for (int i = 0; i < 2 * n; i++)
if (s[i] >= 0) {
int j = p[s[i] < n ? s[i] + n : s[i] - n].back();
change(i, -1);
change(j, -1);
r += (s[i] < n) + sum(j);
p[s[i]].pop_back();
p[s[j]].pop_back();
s[j] = -1;
}
return r;
}
# | 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... |