Submission #626893

#TimeUsernameProblemLanguageResultExecution timeMemory
626893chonkaSwap (BOI16_swap)C++17
48 / 100
1088 ms7288 KiB
#include<bits/stdc++.h> using namespace std ; typedef long long ll ; const int MAXN = 2e5 + 7 ; int n ; int a[ MAXN ] , f[ MAXN ] ; int b[ MAXN ] ; vector < int > sr[ 18 ][ 2 ] ; int rem[ 18 ][ MAXN ] ; void fill ( int x , int lvl ) { rem[ lvl ][ x ] = a[ x ] ; if ( 2 * x <= n ) { fill ( 2 * x , lvl ) ; } if ( 2 * x + 1 <= n ) { fill ( 2 * x + 1 , lvl ) ; } } static void fill_ord ( int x , int lvl , int id ) { int tp = 0 ; sr[ lvl ][ id ][ tp ++ ] = a[ x ] ; a[ x ] = rem[ lvl ][ x ] ; int ori = x ; for ( int aux = 1 ; aux < 20 ; ++ aux ) { x *= 2 ; for ( int j = 0 ; j < ( 1 << aux ) ; ++ j ) { if ( x + j > n ) { return ; } sr[ lvl ][ id ][ tp ++ ] = a[ x + j ] ; a[ x + j ] = rem[ lvl ][ x + j ] ; } } } static void fill_back ( int x , int lvl , int id ) { int tp = 0 ; a[ x ] = sr[ lvl ][ id ][ tp ++ ] ; int ori = x ; for ( int aux = 1 ; aux < 20 ; ++ aux ) { x *= 2 ; for ( int j = 0 ; j < ( 1 << aux ) ; ++ j ) { if ( x + j > n ) { return ; } a[ x + j ] = sr[ lvl ][ id ][ tp ++ ] ; } } } void rec ( int wh , int lvl ) { if ( 2 * wh > n ) { f[ wh ] = 0 ; return ; } if ( 2 * wh + 1 <= n ) { if ( a[ wh ] < a[ 2 * wh ] && a[ wh ] < a[ 2 * wh + 1 ] ) { f[ wh ] = 0 ; } else if ( a[ 2 * wh ] < a[ wh ] && a[ 2 * wh ] < a[ 2 * wh + 1 ] ) { f[ wh ] = 1 ; swap ( a[ wh ] , a[ 2 * wh ] ) ; } else { fill ( wh , lvl ) ; swap ( a[ wh ] , a[ 2 * wh + 1 ] ) ; if ( 2 * wh <= n ) { rec ( 2 * wh , lvl + 1 ) ; } if ( 2 * wh + 1 <= n ) { rec ( 2 * wh + 1 , lvl + 1 ) ; } fill_ord ( wh , lvl , 0 ) ; swap ( a[ wh ] , a[ 2 * wh ] ) ; swap ( a[ wh ] , a[ 2 * wh + 1 ] ) ; if ( 2 * wh <= n ) { rec ( 2 * wh , lvl + 1 ) ; } if ( 2 * wh + 1 <= n ) { rec ( 2 * wh + 1 , lvl + 1 ) ; } fill_ord ( wh , lvl , 1 ) ; bool sw = false ; for ( int i = 1 ; i <= n ; ++ i ) { if ( sr[ lvl ][ 0 ][ i ] != sr[ lvl ][ 1 ][ i ] ) { if ( sr[ lvl ][ 0 ][ i ] < sr[ lvl ][ 1 ][ i ] ) { sw = true ; } break ; } } if ( sw == false ) { f[ wh ] = 3 ; fill_back ( wh , lvl , 1 ) ; } else { f[ wh ] = 2 ; fill_back ( wh , lvl , 0 ) ; } return ; } } else { if ( a[ wh ] < a[ 2 * wh ] ) { f[ wh ] = 0 ; } else { f[ wh ] = 1 ; swap ( a[ wh ] , a[ 2 * wh ] ) ; } return ; } if ( 2 * wh <= n ) { rec ( 2 * wh , lvl + 1 ) ; } if ( 2 * wh + 1 <= n ) { rec ( 2 * wh + 1 , lvl + 1 ) ; } } void solve ( ) { cin >> n ; for ( int i = 1 ; i <= n ; ++ i ) { cin >> a[ i ] ; } for ( int i = 0 ; i < 18 ; ++ i ) { sr[ i ][ 0 ].resize ( ( 1 << ( 18 - i ) ) ) ; sr[ i ][ 1 ].resize ( ( 1 << ( 18 - i ) ) ) ; } rec ( 1 , 0 ) ; 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 ; }

Compilation message (stderr)

swap.cpp: In function 'void fill_ord(int, int, int)':
swap.cpp:25:9: warning: unused variable 'ori' [-Wunused-variable]
   25 |     int ori = x ;
      |         ^~~
swap.cpp: In function 'void fill_back(int, int, int)':
swap.cpp:39:9: warning: unused variable 'ori' [-Wunused-variable]
   39 |     int ori = x ;
      |         ^~~
#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...