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;
long long count_swaps(vector<int> s) {
int n = s.size()/2;
priority_queue<int> L, R;
for (int i = 0; i < 2*n; i++) {
if (s[i] < 0) {
L.push(-i);
} else {
R.push(-i);
}
}
long long swaps = 0;
for (int i = 0; i < 2*n; i++) {
if (s[i] < 0) {
// L
if (i % 2 == 1) {
// Mismatch
int nearest = -R.top();
R.pop();
swaps += nearest-i;
if (!L.empty()) L.pop();
L.push(-nearest);
s[i] *= -1; s[nearest] *= -1;
//cout << 1 << " " << nearest << " " << i << '\n';
} else if (!L.empty()) {
// Correcto
L.pop();
}
} else {
// R
if (i % 2 == 0) {
// Mismatch
int nearest = -L.top();
L.pop();
swaps += nearest - i;
if (!R.empty()) R.pop();
R.push(-nearest);
s[i] *= -1; s[nearest] *= -1;
//cout << nearest << " " << i << '\n';
} else if (!R.empty()) {
// Correcto
R.pop();
}
}
}
return swaps;
}
# | 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... |