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 build(vector<int> &t, vector<int> arr, int v, int l, int r)
{
if (l == r)
{
t[l] = 0;
}
else
{
int mid = (l + r) / 2;
build(t, arr, v * 2, l, mid);
build(t, arr, v * 2 + 1, mid + 1, r);
t[v] = t[v * 2] + t[v * 2 + 1];
}
}
void update(vector<int> &t, int v, int tl, int tr, int l, int r, int V)
{
if (l > r)
{
return;
}
if (tl == l && tr == r)
{
t[v] += V;
}
else
{
int mid = (tl + tr) / 2;
update(t, v * 2, tl, mid, l, min(r, mid), V);
update(t, v * 2 + 1, mid + 1, tr, max(l, mid + 1), r, V);
}
}
int get(vector<int> t, int v, int tl, int tr, int pos)
{
if (tl == tr)
{
return t[v];
}
int mid = (tl + tr) / 2;
if (pos <= mid)
{
return t[v] + get(t, v * 2, tl, mid, pos);
}
else
{
return t[v] + get(t, v * 2 + 1, mid + 1, tr, pos);
}
}
long long count_swaps(vi s)
{
int n = s.size(), l = 0;
long long c = 0;
mii chain, rev;
vector<int> t(n * 4);
unordered_map<int, queue<int>> ref;
build(t, s, 1, 0, n - 1);
for (int i = 0; i < n; i++)
{
ref[s[i]].push(i);
}
for (int i = 0; i < n / 2; i++)
{
int ln = s[l], li = l;
li = ref[-ln].front();
ref[ln].pop();
ref[-ln].pop();
c += li - l - get(t, 1, 0, n - 1, li);
update(t, 1, 0, n - 1, li, n - 1, 1);
if (l + 1 == li)
l += 2;
else
l += 1;
}
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... |