Cod sursa(job #1609397)

Utilizator cristinamateiCristina Matei cristinamatei Data 22 februarie 2016 19:42:12
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N = 100003;
int n, a[N], lung[N], pred[N], lmax, poz, rec[N];

int main()
{
    int i, j;
    in >> n;
    for ( i = 1; i <= n; i++ )
        in >> a[i];
    lung[1] = 1;
    pred[1] = 0;
    for ( i = 2; i <= n; i++ )
    {
        lung[i] = 0;
        pred[i] = 0;
        for ( j = 1; j < i; j++ )
        {
            if ( a[j] < a[i] )
                if ( lung[j] > lung[i] )
                {
                    lung[i] = lung[j];
                    pred[i] = j;
                }
        }
        lung[i]++;
    }
    lmax = lung[1];
    poz = 1;
    for ( i = 2; i <= n; i++ )
        if ( lmax < lung[i] )
        {
            lmax = lung[i];
            poz = i;
        }
    out << lmax<<'\n';
    i = 1;
    while( pred[poz] != 0 )
    {
        rec[i] = poz;
        i++;
        poz = pred[poz];
    }
    rec[i] = poz;
    for ( j = lmax; j >= 1; j-- )
        out << a[rec[j]] <<' ';
    return 0;
}