Cod sursa(job #2342761)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 13 februarie 2019 12:43:49
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>
using namespace std ;
const int NR =  100005 ;
const int oo = ( 1 << 31 ) - 1 ;
ifstream in ( "scmax.in" ) ;
ofstream out ( "scmax.out" ) ;
vector < int > m ( NR , oo ) ;
int a [ NR ] , n , p [ NR ] , r [ NR ] , posj , posmax ;

int dei ( int from , int to , int key )
{
    if ( from - to <= 1 )
    {
        if ( m [ to ] < key )   return to ;
        return from ;
    }
    int mid = ( from + to ) >> 1 ;
    if ( key > m [ mid ] )  return dei ( mid , to , key ) ;
    if ( key < m [  mid ] )  return dei ( from , mid , key ) ;
    if ( key == m [ mid ] ) return dei ( from , mid , key ) ;
}
void print ( int pos )
{
    if ( !pos ) return ;
    print ( r [ pos ] ) ;
    out << a [ pos ] << ' ' ;
}
int main ()
{
    in >> n ;
    for ( int i = 1 ; i <= n ; ++ i )
    {
        in >> a [ i ] ;
        posj = dei ( 0 , posmax , a [ i ] ) ;
        if ( m [ posj + 1 ] > a [ i ] )
        {
            m [ posj + 1 ] = a [ i ] ;
            posmax = max ( posmax , posj + 1 ) ;
            p [ posj + 1 ] = i ;
            r [ i ] = p [ posj ] ;
        }
    }
    out << posmax << '\n' ;
    print( p [ posmax ] ) ;
 }