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;
const int maxn = 1e5 + 20;
int order[maxn * 2];
int sizes[maxn * 2];
int match[maxn * 2];
bool flag[maxn * 2];
int bit[maxn * 2];
vector<int> shoes[maxn][2];
int n;
void update(int pos) {
for (int i = pos; i <= n * 2; i += i & (-i)) {
bit[i]++;
}
}
int get(int pos) {
int res = 0;
for (int i = pos; i > 0; i -= i & (-i)) {
res += bit[i];
}
return res;
}
long long count_swaps(vector<int> _sizes) {
n = (int)_sizes.size() / 2;
for (int i = 1; i <= n * 2; i++) {
sizes[i] = _sizes[i - 1];
if (sizes[i] < 0) {
shoes[-sizes[i]][0].push_back(i);
}
else {
shoes[sizes[i]][1].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < (int)shoes[i][0].size(); j++) {
match[shoes[i][0][j]] = shoes[i][1][j];
match[shoes[i][1][j]] = shoes[i][0][j];
}
}
int cnt = 0;
for (int i = 1; i <= n * 2; i++) {
if (!flag[i]) {
order[i] = cnt + (sizes[i] < 0 ? 1 : 2);
order[match[i]] = cnt + (sizes[match[i]] < 0 ? 1 : 2);
flag[i] = true;
flag[match[i]] = true;
cnt += 2;
}
}
long long res = 0;
for (int i = n * 2; i >= 1; i--) {
res += get(order[i]);
update(order[i]);
}
return res;
}
# | 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... |