Cod sursa(job #1630434)

Utilizator danyvsDan Castan danyvs Data 5 martie 2016 09:02:53
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#define NMax 100001

using namespace std;

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

int a[NMax], n;
int lg[NMax], t[NMax];

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

void Scmax()
{
    int i, j;
    lg[1] = 1;
    t[1] = 0;
    for (i = 1; i <= n; ++ i)
        {
         lg[i] = 1;
         t[i] = 0;
         for (j = 1; j < i; ++ j)
            if (a[j] < a[i] && lg[j] + 1 > lg[i])
                {
                 lg[i] = lg[j] + 1;
                 t[i] = j;
                }
        }
}

void Solutie(int p)
{
    if (t[p] != 0) Solutie(t[p]);
    fout << a[p] << " ";
}

void Afisare()
{
    int lgmax, i, p;
    lgmax = 0;
    for (i = 1; i <= n; ++ i)
        if (lg[i] > lgmax)
            {
             lgmax = lg[i];
             p = i;
            }
    fout << lgmax << "\n";
    Solutie(p);
}

int main()
{
    Citire();
    fin.close();
    Scmax();
    Afisare();
    fout.close();
    return 0;
}