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>
using namespace std;
#include "shoes.h"
long long count_swaps(std::vector<int> a) {
int n = a.size ();
map < int, deque < int > > c;
for (int i = 0; i < n; ++i) c[a[i]].push_back (i);
vector < int > st (n);
for (int i = 0; i < n; ++i) st[i] = (i + 1) & -(i + 1);
auto inc = [&] (int i, int x)
{
while (i < n)
{
st[i] += x;
i += (i + 1) & -(i + 1);
}
};
auto sum = [&] (int i)
{
int ans = 0;
while (i >= 0)
{
ans += st[i];
i -= (i + 1) & -(i + 1);
}
return ans;
};
vector < bool > b (n);
long long ans = 0;
for (int i = 0; i < n; ++i)
{
if (b[i]) continue;
int j = c[-a[i]].front ();
c[-a[i]].pop_front ();
c[a[i]].pop_front ();
ans += sum (j) - sum (i) - 1;
if (a[i] > 0) ++ans;
inc (i, 1);
inc (j, -1);
b[j] = true;
}
return ans;
}
# | 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... |