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>
using namespace std;
#define itr(a) a.begin(), a.end()
typedef vector<int> vi;
typedef unordered_map<int, int> mii;
void move(mii &chain, mii &rev, int a, int b)
{
chain[rev[a]] = chain[a];
rev[chain[a]] = rev[a];
chain[a] = chain[b];
rev[chain[b]] = a;
chain[b] = a;
rev[a] = b;
}
int cost(mii chain, int a, int b, int c = 0)
{
if (a == b)
return c;
c++;
return cost(chain, chain[a], b, c);
}
long long count_swaps(vi s)
{
int n = s.size(), l = 0;
long long c = 0;
mii chain, rev;
chain[0] = s[0];
rev[s[0]] = 0;
for (int i = 0; i < n - 1; i++)
{
chain[s[i]] = s[i + 1];
rev[s[i + 1]] = s[i];
}
int f = *find_if(itr(s), [](int i)
{ return i < 0; });
l = f;
if (f != s[0])
{
c += cost(chain, s[0], f);
move(chain, rev, f, 0);
}
for (int i = 0; i < n / 2; i++)
{
int li = l * -1;
if (l < 0)
{
c += cost(chain, l, li) - 1;
move(chain, rev, li, l);
l = chain[li];
}
else
{
c += cost(chain, l, li);
move(chain, rev, li, rev[l]);
l = chain[l];
}
}
return c;
}
# | 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... |