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;
queue<int> otherpos[100100];
int T[200100];
void Add(int x){
while(x<200100)
T[x]++,x+=x&-x;
}
int Qry(int x){
int ans=0;
while(x)
ans+=T[x],x-=x&-x;
return ans;
}
long long calc(vector<int>v){
long long ans=0;
int K=0;
for(auto i:v) {
Add(i+1);
ans+=K++-Qry(i);
}
return ans;
}
long long count_swaps(std::vector<int> s) {
long long ans=0;
int n=s.size()>>1;
vector<int>sup;
int CC=0;
for(int i=0;i<n*2;i++) if(s[i]<0)
otherpos[-s[i]].push(i);
CC=0;
vector<pair<int,int>> thgs;
for(int i=0;i<n*2;i++) if(s[i]>0) {
int x=otherpos[s[i]].front();
thgs.push_back({x,i});
otherpos[s[i]].pop();
}
for(auto&[i,j]:thgs)
if(i>j)
ans++,swap(i,j);
sort(thgs.begin(),thgs.end());
for(auto[i,j]:thgs)
sup.push_back(i),
sup.push_back(j);
return ans+calc(sup);
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:29:9: warning: variable 'CC' set but not used [-Wunused-but-set-variable]
29 | int CC=0;
| ^~
# | 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... |