답안 #626867

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
626867 2022-08-11T22:04:46 Z chonka Swap (BOI16_swap) C++17
0 / 100
5 ms 9684 KB
#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 ;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -