Submission #626907

#TimeUsernameProblemLanguageResultExecution timeMemory
626907chonkaSwap (BOI16_swap)C++17
100 / 100
746 ms3652 KiB
#include<bits/stdc++.h> using namespace std ; typedef long long ll ; const int MAXN = 2e5 + 7 ; int n ; int a[ MAXN ] ; int get ( int wh , int val ) { if ( 2 * wh > n ) { return wh ; } if ( 2 * wh == n ) { if ( val < a[ 2 * wh ] ) { return wh ; } return 2 * wh ; } if ( val < a[ 2 * wh ] && val < a[ 2 * wh + 1 ] ) { return wh ; } if ( a[ 2 * wh ] < val && a[ 2 * wh ] < a[ 2 * wh + 1 ] ) { return get ( 2 * wh , val ) ; } int x = a[ 2 * wh ] ; if ( x < val ) { int sr1 = get ( 2 * wh , x ) ; int sr2 = get ( 2 * wh + 1 , x ) ; if ( sr1 < sr2 ) { return get ( 2 * wh + 1 , val ) ; } else { return get ( 2 * wh , val ) ; } } else { int sr1 = get ( 2 * wh , val ) ; int sr2 = get ( 2 * wh + 1 , val ) ; return min ( sr1 , sr2 ) ; } } void solve ( ) { cin >> n ; for ( int i = 1 ; i <= n ; ++ i ) { cin >> a[ i ] ; } for ( int i = 1 ; i <= n ; ++ i ) { if ( 2 * i > n ) { continue ; } if ( 2 * i == n ) { if ( a[ i ] > a[ 2 * i ] ) { swap ( a[ i ] , a[ 2 * i ] ) ; } continue ; } if ( a[ i ] < a[ 2 * i ] && a[ i ] < a[ 2 * i + 1 ] ) { continue ; } if ( a[ 2 * i ] < a[ i ] && a[ 2 * i ] < a[ 2 * i + 1 ] ) { swap ( a[ i ] , a[ 2 * i ] ) ; continue ; } swap ( a[ i ] , a[ 2 * i + 1 ] ) ; if ( a[ 2 * i ] < a[ 2 * i + 1 ] ) { int sr1 = get ( 2 * i , a[ 2 * i ] ) ; int sr2 = get ( 2 * i + 1 , a[ 2 * i ] ) ; if ( sr2 < sr1 ) { swap ( a[ 2 * i ] , a[ 2 * i + 1 ] ) ; } } else { int sr1 = get ( 2 * i , a[ 2 * i + 1 ] ) ; int sr2 = get ( 2 * i + 1 , a[ 2 * i + 1 ] ) ; if ( sr1 < sr2 ) { swap ( a[ 2 * i ] , a[ 2 * i + 1 ] ) ; } } } for ( int i = 1 ; i <= n ; ++ i ) { cout << a[ 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...