Submission #626907

# Submission time Handle Problem Language Result Execution time Memory
626907 2022-08-12T00:29:12 Z chonka Swap (BOI16_swap) C++17
100 / 100
746 ms 3652 KB
#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 time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 420 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 420 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 9 ms 724 KB Output is correct
17 Correct 11 ms 744 KB Output is correct
18 Correct 9 ms 724 KB Output is correct
19 Correct 97 ms 772 KB Output is correct
20 Correct 86 ms 1052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 420 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 9 ms 724 KB Output is correct
17 Correct 11 ms 744 KB Output is correct
18 Correct 9 ms 724 KB Output is correct
19 Correct 97 ms 772 KB Output is correct
20 Correct 86 ms 1052 KB Output is correct
21 Correct 44 ms 3652 KB Output is correct
22 Correct 38 ms 3600 KB Output is correct
23 Correct 42 ms 3584 KB Output is correct
24 Correct 710 ms 3632 KB Output is correct
25 Correct 746 ms 3632 KB Output is correct