Cod sursa(job #446887)

Utilizator alexandru92alexandru alexandru92 Data 26 aprilie 2010 20:31:51
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on April 26, 2010, 7:38 PM
 */
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
#define SIZE 100011
#define Nmax 100011

/*
 * 
 */
using namespace std;
ifstream in;
ofstream out;
int N, fidx;
char file[SIZE];
int v[Nmax], v2[Nmax], aib[Nmax], ss[Nmax], f[Nmax];
inline void UpDate( int x, int y )
{
    for( int z=ss[x]; y <= N; y+=( y & -y ) )
        if( ss[aib[y]] < z )
            aib[y]=x;
}
inline int Query( int x )
{
    int mx;
    for( mx=0; x; x-=( x & -x ) )
        if( ss[aib[x]] > ss[mx] )
            mx=aib[x];
    return mx;
}
inline void output( int idx )
{
    if( idx )
    {
        output( f[idx] );
        out<<v[idx]<<' ';
    }
}
inline void read( int& x )
{
    int sign=1;
    while( file[fidx] < '0' || file[fidx] > '9' )
    {
        if( '-' == file[fidx] )
            sign=-1;
        if( ++fidx == SIZE )
        {
            fidx=0;
            in.read( file, SIZE );
        }
    }
    for( x=0; file[fidx] >= '0' && file[fidx] <= '9'; )
    {
        x=x*10+file[fidx]-'0';
        if( ++fidx == SIZE )
        {
            fidx=0;
            in.read( file, SIZE );
        }
    }
    x*=sign;
}
int main(int argc, char** argv)
{
    int i, idx, mx;
    int *start=v2+1, *end;
    in.open( "scmax.in" );
    read(N);
    end=v2+N+1;
    for( i=1; i <= N; ++i )
    {
        read( v[i] );
        v2[i]=v[i];
    }
    sort( start, end );
    for( mx=i=1; i <= N; ++i )
    {
        idx=lower_bound( start, end, v[i] )-start+1;
        f[i]=Query( idx-1 );
        ss[i]=1+ss[f[i]];
        UpDate( i, idx );
        if( ss[i] > ss[mx] )
            mx=i;
    }
    out.open( "scmax.out" );
    out<<ss[mx]<<'\n';
    output( mx );
    return (EXIT_SUCCESS);
}