# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1020253 | abdelhakim | 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>
#define mod 1000000007LL
#define inf 1e17
#define ll long long
using namespace std;
void printvec(vector<int> vec)
{
for (auto &&e : vec)
{
cout << e << ' ';
}
cout << endl;
}
ll count_swaps(vector<int> s)
{
ll n = s.size() / 2;
bool sametype = true;
for (int i = 1; i < n; i++)
{
if(abs(s[i])!=abs(s[i-1]))
{
sametype = false;
break;
}
}
if(n == 1) return s[0] > s[1];
else if(n <= 8)
{
ll ans = inf;
vector<pair<ll, ll>> perm;
for (int i = 0; i < 2 * n; i++)
{
if (s[i] > 0)
{
perm.push_back({-s[i], s[i]});
}
}
sort(perm.begin(), perm.end());
vector<int> temp = s;
do
{
s = temp;
ll subans = 0;
vector<int> v;
for (int i = 0; i < n; i++)
{
v.push_back(perm[i].first);
v.push_back(perm[i].second);
}
map<ll,vector<ll>> index;
for (int i = 0; i < 2*n; i++)
{
index[v[i]].push_back(i);
}
vector<pair<ll,ll>> arr;
for (int i = 0; i < 2*n; i++)
{
arr.push_back({s[i],index[s[i]][0]});
index[s[i]].erase(index[s[i]].begin());
}
// for (int i = 0; i< 2*n; i++)
// {
// cout << arr[i].first << ' '<< arr[i].second << endl;
// }
for (int i = 1; i < 2*n; i++)
{
for (int j = 0; j < i; j++)
{
if(arr[j].second > arr[i].second)subans++;
}
}
ans = min(ans, subans);
} while (next_permutation(perm.begin(), perm.end()));
return ans;
}
else if(!sametype) return n*(n-1)/2;
else
{
ll ans = inf;
vector<pair<ll, ll>> perm;
for (int i = 0; i < 2 * n; i++)
{
if (s[i] > 0)
{
perm.push_back({-s[i], s[i]});
}
}
sort(perm.begin(), perm.end());
vector<int> temp = s;
do
{
s = temp;
ll subans = 0;
vector<int> v;
for (int i = 0; i < n; i++)
{
v.push_back(perm[i].first);
v.push_back(perm[i].second);
}
for (int i = 0; i < 2 * n; i++)
{
if (s[i] != v[i])
{
for (int j = i + 1; j < 2 * n; j++)
{
if (v[j] == s[j - 1] && s[j] != s[j - 1])
{
swap(s[j], s[j - 1]);
subans++;
i = -1; // This will reset the outer loop
break;
}
swap(s[j], s[j - 1]);
if (s[j] != s[j - 1]) subans++;
}
}
}
ans = min(ans, subans);
} while (next_permutation(perm.begin(), perm.end()));
return ans;
}
}
int main()
{
cout << count_swaps({-1, 1, -1, -1, -1, 1, 1, 1}) << endl;
}