이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |