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>
using namespace std ;
typedef long long ll ;
const int MAXN = 2e5 + 7 ;
int n ;
int a[ MAXN ] , ans[ MAXN ] , pos[ MAXN ] ;
bool used[ MAXN ] , right[ MAXN ] ;
set < int > s[ MAXN ] ;
void solve ( ) {
cin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
pos[ a[ i ] ] = i ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
s[ i ].insert ( a[ i ] ) ;
if ( 2 * i <= n ) {
s[ i ].insert ( a[ 2 * i ] ) ;
}
if ( 2 * i + 1 <= n ) {
s[ i ].insert ( a[ 2 * i + 1 ] ) ;
}
}
for ( int i = 1 ; i <= n ; ++ i ) {
int act = 0 ;
while ( 1 ) {
act = *s[ i ].begin ( ) ;
s[ i ].erase ( act ) ;
if ( used[ act ] == false ) { break ; }
}
ans[ i ] = act ;
used[ act ] = true ;
if ( pos[ act ] == 2 * i + 1 ) {
for ( auto x : s[ i ] ) {
s[ 2 * i ].insert ( x ) ;
s[ 2 * i + 1 ].insert ( x ) ;
}
}
else if ( pos[ act ] == 2 * i ) {
for ( auto x : s[ i ] ) {
if ( pos[ x ] != 2 * i + 1 ) {
s[ 2 * i ].insert ( x ) ;
}
}
}
}
for ( int i = 1 ; i <= n ; ++ i ) {
cout << ans[ i ] << " " ;
}
cout << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 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... |