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<bits/stdc++.h>
#include "shoes.h"
using namespace std;
int n , pos , indx , m , x , y , z ;
stack < int > r [ 200005 ] ;
stack < int > l [ 200005 ] ;
int a [ 200005 ] ;
int bit [ 200005 ] ;
bool vis [ 200005 ] ;
void add ( int indx ) {
while ( indx <= n ) {
bit [ indx ] ++ ;
indx += indx & ( -indx ) ;
}
}
int get ( int indx ) {
int sum = 0 ;
while ( indx > 0 ) {
sum += bit [ indx ] ;
indx -= indx & ( -indx ) ;
}
return sum ;
}
int move ( int indx , int pos ) {
vis [ indx ] = 1 ;
add ( indx ) ;
return ( pos - ( indx - get ( indx - 1 ) ) ) ;
}
long long count_swaps( vector<int> arr ) {
n = arr . size ( ) ;
for ( int i = 1 ; i <= n ; i ++ ) {
a [ i ] = arr [ i - 1 ] ;
}
for ( int i = 1 ; i <= n ; i ++ ) {
if ( a [ i ] < 0 ) {
l [ -a [ i ] ] . push ( i ) ;
}
else {
r [ a [ i ] ] . push ( i ) ;
}
}
long long ans = 0 ;
x = n ;
for ( int i = n ; i >= 1 ; i -- ) {
if ( vis [ i ] == 0 ) {
y = abs ( a [ i ] ) ;
ans += move ( r [ y ] . top ( ) , x ) ;
ans += move ( l [ y ] . top ( ) , x - 1 ) ;
r [ y ] . pop ( ) ;
l [ y ] . pop ( ) ;
x -= 2 ;
}
}
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... |