Cod sursa(job #1825796)

Utilizator vlasiuflaviusVlasiu Flavius vlasiuflavius Data 9 decembrie 2016 17:29:39
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <iostream>
#define x first
#define y second
using namespace std;
ofstream fout ("scmax.out");
ifstream fin ("scmax.in");
int n,v[100100],ind[100100],i,poz,a,ppdd[100100],pozcrt,crt;
pair < int , int > p[100100];
int caut( int poz )
{
    int cs = 1;
    int cd = poz;
    int rsp = 1000000;
    while( cs <= cd )
    {
        int mij = ( cs + cd ) >> 1;
        if( p[ mij ].x >= v[ i ] )
        {
            cd = mij - 1;
            rsp = mij;
        }
        else
            cs = mij + 1;
    }
    return rsp;
}
int main()
{
    fin>>n;
    for( i = 1 ;  i <= n ; i++ )
        fin>>v[ i ];
    p[ 1 ] = make_pair( v[ 1 ] , 1 );
    poz = 1;
    for( i = 2 ; i <= n ; i++ )
    {
        a = caut( poz );
        if( a == 1000000 )
        {
            p[ ++poz ] = make_pair( v[ i ] , i );
            ind[ i ] = p[ poz - 1 ].y;
        }
        else
        {
            p[ a ] = make_pair( v[ i ] , i );
            ind[ i ] = p[ a - 1 ].y;
        }
    }
    pozcrt = poz;
    poz = p[ poz ].y;
    while( poz )
    {
        ppdd[ ++crt ] = v[ poz ];
        poz = ind[ poz ];
    }
    fout<<pozcrt<<'\n';
    for( i = 1 ; i <= pozcrt ; i++ )
        fout<<ppdd[ pozcrt - i + 1 ]<<" ";
}