# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
222864 | MrDomino | Arranging Shoes (IOI19_shoes) | C++14 | 1022 ms | 3700 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"
using namespace std;
typedef long long ll;
ll get_dist(vector<int> ord)
{
ll sol = 0;
for (int i = 0; i < (int) ord.size(); i++)
{
sol += abs(ord[i] - i);
}
return sol / 2;
}
const int N = (int) 1e5 + 7;
int pl[N];
int mn[N];
long long count_swaps(vector<int> a)
{
int n = (int) a.size() / 2;
bool sim_sub = 1;
for (int i = 0; i < n; i++)
{
sim_sub &= (a[i] < 0 && a[i + n] > 0 && a[i] == -a[i + n]);
}
if (sim_sub)
{
vector<int> ord;
for (int i = 0; i < n; i++)
{
ord.push_back(i);
ord.push_back(i + n);
}
return get_dist(ord);
}
ll sol = 0;
for (int i = 0; i < n; i++)
{
int pos = 2 * i;
for (int j = 1; j <= n; j++)
{
pl[j] = mn[j] = (int) 1e9;
}
for (int j = pos; j < 2 * n; j++)
{
if (a[j] < 0)
{
mn[-a[j]] = min(mn[-a[j]], j);
}
if (a[j] > 0)
{
pl[a[j]] = min(pl[a[j]], j);
}
}
int smin = (int) 1e9;
for (int j = 1; j <= n; j++)
{
smin = min(smin, pl[j] + mn[j]);
}
int init = sol;
for (int j = 1; j <= n; j++)
{
if (pl[j] + mn[j] == smin)
{
int val = mn[j];
while (val > pos)
{
swap(a[val], a[val - 1]);
val--;
sol++;
}
pos++;
if (a[pl[j]] != j)
{
pl[j]++;
}
val = pl[j];
while (val > pos)
{
swap(a[val], a[val - 1]);
val--;
sol++;
}
break;
}
}
}
return sol;
}
Compilation message (stderr)
# | 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... |