Pagini recente » Cod sursa (job #3235547) | Cod sursa (job #2524286) | Cod sursa (job #2305607) | Cod sursa (job #638783) | Cod sursa (job #2333404)
#include <fstream>
using namespace std;
ifstream fin( "scmax.in" );
ofstream fout( "scmax.out" );
const int NMAX = 100005;
const int INF = 2000000000;
int N;
int a[NMAX];
int dp[NMAX];
int pre[NMAX];
int aux[NMAX];
void Read()
{
fin >> N;
for( int i = 1; i <= N; ++i )
fin >> a[i];
fin.close();
}
int BinaryS( int lf, int rg, int val )
{
if( lf > rg ) return -INF;
int mid = ( lf + rg ) / 2;
if( a[ aux[mid] ] < val )
return max( mid, BinaryS( mid + 1, rg, val ) );
else
return BinaryS( lf, mid - 1, val );
}
void PrintRev( int pos )
{
if( pre[pos] > 0 ) PrintRev( pre[pos] );
fout << a[pos] << ' ';
}
void Do()
{
a[0] = -INF;
aux[0] = 0;
for( int i = 1; i <= N; ++i )
aux[i] = INF;
int prev;
int last = 0;
for( int i = 1; i <= N; ++i )
{
prev = BinaryS( 0, last, a[i] );
pre[i] = aux[prev];
dp[i] = dp[ aux[prev] ] + 1;
aux[ prev + 1 ] = i;
last = max( last, prev + 1 );
}
int lg_max = 0, pos_max;
for( int i = 1; i <= N; ++i )
if( lg_max < dp[i] )
{
lg_max = dp[i];
pos_max = i;
}
fout << lg_max << '\n';
PrintRev( pos_max );
}
int main()
{
Read();
Do();
return 0;
}