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;
const int MX = 2e5 + 5;
int a[MX], used[MX];
void add(int ind, int x)
{
ind++;
while(ind < MX)
{
a[ind] += x;
ind += ind & -ind;
}
}
int get(int ind)
{
ind++;
int r = 0;
while(ind > 0)
{
r += a[ind];
ind -= ind & -ind;
}
return r;
}
long long count_swaps(vector<int> s) {
int N = s.size() / 2;
queue<int> p[N + 1], n[N + 1];
for(int i = 0; i < N * 2; i++)
{
if(s[i] > 0)p[s[i]].push(i + 1);
else n[-s[i]].push(i + 1);
}
long long ans = 0;
for(int i = 0; i < N * 2; i++)
{
if(used[i + 1])continue;
int x = p[abs(s[i])].front();
int y = n[abs(s[i])].front();
p[abs(s[i])].pop();
n[abs(s[i])].pop();
if(s[i] > 0)
{
ans += y - x - (get(y) - get(x));
add(y, 1);
}
else
{
ans += x - y - (get(x) - get(y)) - 1;
add(x, 1);
}
used[y] = 1;
used[x] = 1;
}
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... |