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;
int Fenwick[200010], n;
void update(int pos, int val) { while(pos<=200000)Fenwick[pos]+=val, pos+=pos&-pos; }
int sum(int x) { int S = 0; while(x)S+=Fenwick[x],x-=x&-x; return S; }
int query(int l, int r) { return sum(r)-sum(l-1); }
long long count_swaps(vector<int> s)
{
n = (int)s.size()/2;
vector<pair<int,int> > ord[200010], v;
for(int i = 0; i < n; i++) ord[abs(s[i])].push_back({s[i],i});
long long ans = 0;
for(int i = 1; i<=n; i++)
{
sort(ord[i].begin(), ord[i].end());
for(int j = 0; j < n/2; j++)
{
int l = ord[i][j].second, r = ord[i][j+n/2].second;
if(l>r) ans++, swap(l,r);
v.push_back({l+1,r+1});
}
}
sort(v.begin(),v.end());
for(int i = 1; i <= 2*n; i++) update(i,1);
for(auto u : v)
{
int l = u.first, r = u.second;
ans+=query(l,r), update(l,-1),update(r,-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... |