Cod sursa(job #1814267)

Utilizator teodoramusatoiuTeodora Musatoiu teodoramusatoiu Data 23 noiembrie 2016 20:05:10
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, m, v[100005], x[100005], pred[100005];

int caut(int i)
{
    int pas = 1 << 16, j = 0;
    while( pas != 0 )
    {
        if ( j+pas<=m&& v[x[j+pas]]<i )
            j+=pas;
        pas/=2;
    }
    return j;
}

void afis(int i)
{
    if(pred[i]>0)
        afis(pred[i]);
    out<<v[i]<<" ";
}

int main()
{
    int i,j;
    m=0;
    in>>n;
    for(i=1;i<=n;i++)
        in>>v[i];
    x[++m]=1;
    for(i=2;i<=n;i++)
    {
        j=caut(v[i]);
        pred[i]=x[j];
        x[j+1]=i;
        if(j+1 > m)
            m++;
    }

    out<<m<<'\n';
    afis(x[m]);

    return 0;
}