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;
typedef long long ll;
#define MAXN 100005
int N;
map<int,vector<int>> emf;
int resp[MAXN];//start position of respective pair
bool used[MAXN];
int bit[MAXN];
ll ans;
int lsone(int x){
return x&(-x);
}
void update(int pos){
pos++;
for(int x=pos;x<=N;x+=lsone(x)){
bit[x]++;
}
}
int query(int pos){
pos++;
int res=0;
for(int x=pos;x>0;x-=lsone(x)){
res+=bit[x];
}
return res;
}
long long count_swaps(std::vector<int> s) {
N=s.size();
for(int i=N-1;i>=0;i--){
emf[s[i]].push_back(i);
}
for(int i=0;i<N;i++){
if(used[i]) continue;
resp[i]=emf[-s[i]].back();
emf[-s[i]].pop_back();
emf[s[i]].pop_back();
used[i]=true;
used[resp[i]]=true;
}
memset(used,false,sizeof(used));
for(int i=0;i<N;i++){
if(used[i]) continue;
ll pos1=i-query(i-1);
ll pos2=resp[i]-query(resp[i]-1);
ans+=(pos2-pos1+((s[i]<0)?-1:0));
update(i);
update(resp[i]);
used[i]=true;
used[resp[i]]=true;
}
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... |