Cod sursa(job #1342567)

Utilizator Leonard1998Olariu Leonard Leonard1998 Data 14 februarie 2015 11:07:13
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#define NMAX 100001

using namespace std;

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

int a[NMAX], lg[NMAX], urm[NMAX];
int n;

void citire ();
void PD ();
void afisare ();

int main()
{
    citire ();
    PD ();
    afisare ();
    fin.close ();
    fout.close ();
    return 0;
}

void citire ()
{
    int i;
    fin >> n;
    for (i = 1; i <= n; i++) fin >> a[i];
}

void PD ()
{
    int i, j, jmax, maxim;
    lg[n] = 1; urm[n] = 0;
    for (i = n-1; i > 0; i--)
    {
        jmax = 0; maxim = 1;
        for (j = i+1; j <= n; j++)
            if (a[i] < a[j] && 1 + lg[j] > maxim)
            {
                maxim = 1 + lg[j];
                jmax = j;
            }
        lg[i] = maxim; urm[i] = jmax;
    }
}

void afisare ()
{
    int i, lgmax, imax;
    lgmax = lg[1]; imax = 1;
    for (i = 2; i <= n; i++)
        if (lgmax < lg[i])
        {
            lgmax = lg[i];
            imax = i;
        }
    fout << lgmax << '\n';

    //reconstituim subsirul
    for (i = imax; i; i = urm[i])
        fout << a[i] << ' ';
    fout << '\n';
}