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;
long long bit[200020];
void add(int pos){
for (int i=pos; i<200020; i+=i&(-i)) bit[i]++;
}
long long query(int pos){
long long ans=0;
for (int i=pos; i; i-=i&(-i)) ans+=bit[i];
return ans;
}
long long count_swaps(std::vector<int> s) {
int n=s.size();
map <int,vector <int> > mp;
for (int i=0; i<n; i++) mp[s[i]].push_back(i);
for (auto&i:mp) reverse(i.second.begin(),i.second.end());
int d[n];
for (int i=0; i<n; i++) d[i]=0;
int cur=1;
for (int i=0; i<n; i++){
if (d[i]) continue;
if (s[i]<0){
mp[s[i]].pop_back();
d[i]=cur;
d[mp[-s[i]].back()]=cur+1;
mp[-s[i]].pop_back();
} else {
mp[s[i]].pop_back();
d[i]=cur+1;
d[mp[-s[i]].back()]=cur;
mp[-s[i]].pop_back();
}
cur+=2;
}
long long ans=0;
for (int i=s.size()-1; i>=0; i--){
ans+=query(d[i]-1);
add(d[i]);
}
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... |