Pagini recente » Cod sursa (job #2564743) | IAP #3: Infoarena3 | Cod sursa (job #2441271) | Monitorul de evaluare | Cod sursa (job #705525)
Cod sursa(job #705525)
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011
using namespace std;
vector< int > v, vSort;
int f[N_MAX], l[N_MAX], aib[N_MAX], index[N_MAX];
void Update( const int& N, int xIndex, int xSIndex )
{
for( ; xSIndex <= N; xSIndex+=( xSIndex & -xSIndex ) )
if( l[aib[xSIndex]] < l[xIndex] )
aib[xSIndex]=xIndex;
}
int Query( int xIndex )
{
int mIndex=0;
for( ; xIndex; xIndex-=( xIndex & -xIndex ) )
if( l[aib[xIndex]] > l[mIndex] )
mIndex=aib[xIndex];
return mIndex;
}
inline void Output( ostream& out, int x )
{
if( 0 == x )
return ;
Output( out, f[x] );
out<<v[x]<<' ';
}
int main()
{
int N, i, mIndex=0;
ifstream in( "scmax.in" );
ofstream out( "scmax.out" );
in>>N;
v.push_back(0);
copy( istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v) );
vSort=v;
sort( vSort.begin(), vSort.end() );
for( i=1; i <= N; ++i )
index[i]=lower_bound( vSort.begin(), vSort.end(), v[i] ) - vSort.begin();
for( i=1; i <= N; ++i )
{
f[i]=Query( index[i]-1 );
l[i]=1+l[f[i]];
if( l[i] > l[mIndex] )
mIndex=i;
Update( N, i, index[i] );
}
out<<l[mIndex]<<'\n';
Output( out, mIndex );
return EXIT_SUCCESS;
}