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;
unordered_map<int, priority_queue<int>> ref;
chain[-1] = 0;
rev[0] = -1;
for (int i = 0; i < n; i++)
{
if (i != n - 1)
{
chain[i] = i + 1;
rev[i + 1] = i;
}
ref[s[i]].push(-i);
}
int fi = *find_if(itr(s), [](int i)
{ return i < 0; });
int f = -ref[fi].top();
l = f;
if (fi != s[0])
{
c += f;
move(chain, rev, f, -1);
}
for (int i = 0; i < n / 2; i++)
{
int ln = s[l], li;
li = -ref[-ln].top();
ref[ln].pop();
ref[-ln].pop();
if (ln < 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... |