Cod sursa(job #483374)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 8 septembrie 2010 13:13:08
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#define nmax 100001
#define INF 0x3f3f3f

using namespace std;

ifstream in("scmax.in");
ofstream out("scmax.out");

int V[nmax],T[nmax],P[nmax];
int L,N;

void afisare(int p)
{
    if(T[p])afisare(T[p]);
    out<<V[p]<<' ';
}

int bs(int key)
{
    int st = 1 ,dr = L,m,lmax=0;
    while(st<=dr)
    {
        m = (st+dr)/2;
        if(V[P[m]]<key)
        {
           lmax = m;
           st = m+1;
        }
        else
            dr = m-1;
    }
    return lmax;
}

int main()
{
    int lg,i;
    in>>N;
    for(i=1;i<=N;i++)
        in>>V[i];
    P[L=1] = 1 ;
    for(i=2;i<=N;i++)
    {
       lg = bs(V[i]);
       if(!P[lg+1])P[lg+1]=i,L++;
       else if(V[P[lg+1]]>V[i])P[lg+1] = i;
       T[i] = P[lg];
    }
    out<<L<<'\n';
    afisare(P[L]);
    return 0;
}
/*
http://infoarena.ro/job_detail/256866?action=view-source
http://infoarena.ro/job_detail/256853?action=view-source
http://infoarena.ro/job_detail/215282?action=view-source
*/