# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1215755 | szz0li | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
long long count_swaps(vector<int> s) {
int ans=INT_MAX;
int n=s.size()/2;
vector<int> szamok;
for(int i=0;i<2*n;i++){
szamok.push_back(s[i]);
}
sort(szamok.begin(), szamok.end());
vector<int> perm(n);
for(int i=0;i<n;i++){
perm[i]=abs(szamok[i]);
}
reverse(perm.begin(), perm.end());
do{
int cnt=0;
vector<int> helyes(2*n), copi=s;
for(int i=0;i<n;i++){
helyes[2*i]=-1*perm[i];
helyes[2*i+1]=perm[i];
}
for(int i=0;i<2*n){
for(int j=i;j<2*n){
if(copi[j]==helyes[i]){
while(j!=i){
swap(copi[j], copi[j-1]);
j--;
cnt++;
}
}
}
}
ans=min(ans, cnt);
}
while(next_permutation(perm.begin(), perm.end())){
int cnt=0;
vector<int> helyes(2*n), copi=s;
for(int i=0;i<n;i++){
helyes[2*i]=-1*perm[i];
helyes[2*i+1]=perm[i];
}
for(int i=0;i<2*n){
for(int j=i;j<2*n){
if(copi[j]==helyes[i]){
while(j!=i){
swap(copi[j], copi[j-1]);
j--;
cnt++;
}
}
}
}
ans=min(ans, cnt);
}
return ans;
}