# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1014343 | KasymK | Arranging Shoes (IOI19_shoes) | C++17 | 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"
using namespace std;
#define ll long long
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
ll count_swaps(vector<int> v){
int n = (int)v.size(), ok = 1;
n >>= 1;
for(int i = 0; i < n; ++i)
ok &= (v[i] < 0);
for(int i = 0; i < n; ++i)
ok &= (-v[i] == v[i+n]);
ll ans = 0;
if(ok){
n--;
ans = n*1ll*(n+1)/2;
return ans;
}
n <<= 1;
for(int i = 0; i < n-1; i+=2){
if(-v[i] == v[i+1]){
// it means these two shoes are the same
if(v[i] > 0)
swap(v[i], v[i+1]), ans++;
}
int ad;
for(int j = i+1; j < n; ++j)
if(-v[i] == v[j]){
ad = j;
break;
}
for(int j = ad; j > i+1; --j)
swap(v[j], v[j-1]), ans++;
if(v[i] > 0)
swap(v[i], v[i+1]), ans++; // it means first shoe is right part
}
return ans;
}
int main(){
ll answer = count_swaps({2, 1, -1, -2});
printf("%lld\n", answer);
}