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 <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
long long count_swaps(vector<int> S){
int n = S.size() / 2;
assert(n <= 8);
vector<int> p;
for (int i = 0; i < n * 2; i++){
if (S[i] > 0){
p.push_back(S[i]);
}
}
sort(p.begin(), p.end());
int ans = INF;
while (true){
map<int, queue<int>> Q;
for (int i = 0; i < n; i++){
Q[-p[i]].push(i * 2);
Q[p[i]].push(i * 2 + 1);
}
vector<int> p2(n * 2);
for (int i = 0; i < n * 2; i++){
p2[i] = Q[S[i]].front();
Q[S[i]].pop();
}
int cnt = 0;
for (int i = 0; i < n * 2; i++){
for (int j = i + 1; j < n * 2; j++){
if (p2[i] > p2[j]){
cnt++;
}
}
}
ans = min(ans, cnt);
if (!next_permutation(p.begin(), p.end())){
break;
}
}
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... |