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;
int bit[200005];
void add(int x,int v){
for (int i=x; i<200005; i+=(i&-i)) bit[i]+=v;
}
int query(int x){
int ans=0;
for (int i=x; i; i-=(i&-i)) ans+=bit[i];
return ans;
}
long long count_swaps(vector <int> s){
int n=(s.size()>>1),ans=0;
vector <pair <int,int> > shs[100001];
vector <pair <int,int> > q;
for (int i=0; i<n+n; i++) shs[abs(s[i])].push_back({s[i],i});
for (int i=1; i<100001; i++){
sort(shs[i].begin(),shs[i].end());
int m=(shs[i].size()>>1);
for (int j=0; j<m; j++){
int ff=shs[i][j].second,ss=shs[i][j+m].second;
if (ff>ss){
swap(ff,ss);
ans++;
}
q.push_back({ff+1,ss+1});
}
}
sort(q.begin(),q.end());
for (int i=1; i<=n+n; i++) add(i,1);
for (auto&i:q){
ans+=query(i.second-1)-query(i.first);
add(i.first,-1);
add(i.second,-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... |