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;
const int N = 1000*101;
int t[4*N];
void update(int v,int s,int e,int l,int r,int val){
if(l>r) return;
if(s == l && e == r){
t[v] = val;
return;
}
int m = (s+e)/2;
update(v*2,s,m,l,min(m,r),val);
update(v*2+1,m+1,e,max(l,m+1),r,val);
t[v] = t[v*2]+t[v*2+1];
}
int get(int v,int s,int e,int l,int r){
if(l>r) return 0;
if(s == l && e == r) return t[v];
int m = (s+e)/2;
return get(v*2,s,m,l,min(m,r))+
get(v*2+1,m+1,e,max(l,m+1),r);
}
long long count_swaps(std::vector<int> s) {
long long ans = 0;
map<int,list<int> > mp;
map<int,int> kl;
int n = s.size();
for(int i = 0;i<s.size();i++){
if(mp[-s[i]].size()){
kl[mp[-s[i]].front()] = i;
mp[-s[i]].pop_front();
continue;
}
mp[s[i]].push_back(i);
}
map<int,bool> don;
for(int i = 0;i<s.size();i++){
if(don[i]) continue;
if(!kl[i]) continue;
if(s[i]>0) ans++;
int pos = kl[i];
int cnt = get(1,0,n-1,i+1,pos);
int dist = pos-i-1;
dist-=cnt;
ans+=dist;
update(1,0,n-1,pos,pos,1);
don[pos] = 1;
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:29:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<s.size();i++){
~^~~~~~~~~
shoes.cpp:38:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<s.size();i++){
~^~~~~~~~~
# | 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... |