#ifdef DEBUG #define _GLIBCXX_DEBUG #define CERR( ANSWER ) cerr << ANSWER << "\n"; #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define CERR( ANSWER ) #endif #include using namespace std; using ll = long long; #define TYPE_OF( VAR ) remove_const::type >::type #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define CEXPR( LL , BOUND , VALUE ) constexpr const LL BOUND = VALUE #define CIN( LL , A ) LL A; cin >> A #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX ) #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ ) #define FOREQINV( VAR , INITIAL , FINAL ) for( TYPE_OF( INITIAL ) VAR = INITIAL ; VAR >= FINAL ; VAR -- ) #define FOR_ITR( ARRAY , ITR , END ) for( auto ITR = ARRAY .begin() , END = ARRAY .end() ; ITR != END ; ITR ++ ) #define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES ) #define QUIT return 0 #define COUT( ANSWER ) cout << ( ANSWER ) << "\n" #define RETURN( ANSWER ) COUT( ANSWER ); QUIT int main() { UNTIE; CEXPR( int , bound_N , 200000 ); CIN_ASSERT( N , 1 , bound_N ); CEXPR( int , bound_ABi , 1000000000 ); static set AB[bound_N] = {}; map > Cmin{}; FOR( i , 0 , N ){ CIN_ASSERT( Ai , 0 , bound_ABi ); assert( Ai % 2 == 0 ); AB[i].insert( Ai ); Cmin[Ai].insert( i ); } int Cmax = 0; FOR( i , 0 , N ){ CIN_ASSERT( Bi , 0 , bound_ABi ); assert( Bi % 2 == 0 ); set& ABi = AB[i]; const int& Ai = *ABi.begin(); int mid = ( Ai + Bi ) / 2; ABi.insert( mid ); Cmin[mid].insert( i ); ABi.insert( Bi ); Cmin[Bi].insert( i ); Ai < Bi ? Cmax < Ai ? Cmax = Ai : Cmax : Cmax < Bi ? Cmax = Bi : Cmax; } int answer = bound_ABi; auto itr_Cmin = Cmin.begin(); while( ! Cmin.empty() ){ int temp = Cmax - itr_Cmin->first; CERR( Cmax << "-" << itr_Cmin->first << "=" << temp ); answer > temp ? answer = temp : answer; FOR_ITR( itr_Cmin->second , itr_i , end_i ){ set& ABi = AB[*itr_i]; if( ABi.size() == 1 ){ RETURN( answer ); } auto itr_ABi = ABi.begin(); itr_ABi = ABi.erase( itr_ABi ); CERR( Cmax << "<" << *itr_ABi << "?" ); Cmax < *itr_ABi ? Cmax = *itr_ABi : Cmax; } itr_Cmin = Cmin.erase( itr_Cmin ); } QUIT; }