# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
807720 | Essa2006 | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 KiB |
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>
//#include "shoes.h"
#include <cstdio>
#include <cassert>
using namespace std;
#define ll long long
#define endl '\n'
#define FF firtst
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
ll count_swaps(vector<int> S){
bool same=1;
int n=S.size();
for(int i=1;i<n;i++){
if(abs(S[i])!=abs(S[i-1]))
same=0;
}
if(n/2<=1000){
int ans=0;
for(int i=0;i<n;i++){
if(S[i]>0){
bool found=0;
int ind=0;
for(int j=0;j<i;j++){
if(-S[j]==S[i] && S[j+1]!=-S[j]){
found=1;
ind=j;
break;
}
}
if(found){
for(int j=i-1;j>ind;j--){
swap(S[j], S[j+1]);
ans++;
}
}
}
else if(S[i]<0){
bool found=0;
int ind=0;
for(int j=0;j<i;j++){
if(-S[j]==S[i] && (!j || S[j]!=-S[j-1])){
found=1;
ind=j;
break;
}
}
if(found){
for(int j=i-1;j>=ind;j--){
swap(S[j], S[j+1]);
ans++;
}
}
}
}
return ans;
}
if(n/2>1000 && !same){
ll ans=S.size()/2;
return ans*(ans-1)/2;
}
}
int main() {
int n;
assert(1 == scanf("%d", &n));
vector<int> S(2 * n);
for (int i = 0; i < 2 * n; i++)
assert(1 == scanf("%d", &S[i]));
fclose(stdin);
long long result = count_swaps(S);
printf("%lld\n", result);
fclose(stdout);
return 0;
}