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>
#include <cstdio>
#include <cassert>
#define all(x) x.begin(),x.end()
using namespace std;
long long count_swaps(std::vector<int> s) {
int n = (int)s.size();
if(n <= 16){
vector<int> neg;
for(int i = 0; i < n; i++){
if(s[i] < 0){
neg.push_back(i);
}
}
const int MAX = 16;
bitset<MAX> mark;
int ans = INT32_MAX;
do{
mark.reset();
vector<int> ar(n);
int aux = 0;
for(int i = 0; i < n/2; i++){
ar[aux] = neg[i];
for(int j = 0; j < n; j++){
if(mark[j] == false and s[j] == -s[ar[aux]]){
ar[aux+1] = j;
mark[j] = true;
break;
}
}
aux += 2;
}
vector<int> pos(n), num(n);
for(int i = 0; i < n; i++) pos[i] = num[i] = i;
int swp = 0;
for(int i = 0; i < n; i++){
swp += abs(i-pos[ar[i]]);
if(pos[ar[i]] < i){
for(int j = pos[ar[i]]+1; j <= i; j++){
pos[ num[j] ]--;
}
pos[ ar[i] ] = i;
}
if(i < pos[ar[i]]){
for(int j = pos[ar[i]]-1; j >= i; j--){
pos[num[j]]++;
}
pos[ ar[i] ] = i;
}
for(int i = 0; i < n; i++) num[pos[i]] = i;
}
ans = min(ans,swp);
}while(next_permutation(all(neg)));
return ans;
}
else{
queue<int> neg, pos;
for(int i = 0; i < n; i++){
if(s[i] < 0) neg.push(i);
else pos.push(i);
}
vector<int> ar(n);
for(int i = 0; i < n-1; i+=2){
ar[i] = neg.front(); neg.pop();
ar[i+1] = pos.front(); pos.pop();
}
vector<int> position(n), num(n);
for(int i = 0; i < n; i++) position[i] = num[i] = i;
int swp = 0;
for(int i = 0; i < n; i++){
swp += abs(i-position[ar[i]]);
if(position[ar[i]] < i){
for(int j = position[ar[i]]+1; j <= i; j++){
position[ num[j] ]--;
}
position[ ar[i] ] = i;
}
if(i < position[ar[i]]){
for(int j = position[ar[i]]-1; j >= i; j--){
position[num[j]]++;
}
position[ ar[i] ] = i;
}
for(int i = 0; i < n; i++) num[position[i]] = i;
}
return swp;
}
return 0;
}
# | 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... |