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 <iostream>
#include <queue>
#define MAX 200000
using namespace std;
///ifstream cin ("c.in");
///ofstream cout ("c.out");
queue <int> pos[MAX + 10];
int tree[MAX + 10], visited[MAX + 10];
void update(int pos, int val)
{
for (int i = pos; i <= MAX; i = i + (i & -i))
tree[i] = tree[i] + val;
}
int query(int pos)
{
int ans = 0;
for (int i = pos; i >= 1; i = i - (i & -i))
ans = ans + tree[i];
return ans;
}
long long count_swaps(vector <int> v)
{
for (int i = 1; i <= MAX; i++)
tree[i] = (i & -i);
int n = v.size();
v.insert(v.begin(), 0);
for (int i = 1; i <= n; i++)
{
if (v[i] < 0)
v[i] = v[i] + n + 1;
pos[v[i]].push(i);
}
long long ans = 0;
for (int i = 1; i <= n; i++)
if (visited[i] == 0)
{
int current = v[i], next = n - v[i] + 1;
if (v[i] > n / 2)
ans--;
int nextpos = pos[next].front();
pos[next].pop();
pos[current].pop();
ans = ans + query(nextpos) - query(i);
update(nextpos, -1);
visited[nextpos] = 1;
visited[i] = 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... |