Cod sursa(job #1966235)

Utilizator nicu_serteSerte Nicu nicu_serte Data 15 aprilie 2017 00:17:02
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
#define nmax 100005
int n, v[nmax], best[nmax], nxt[nmax], lmax, first;
void citire()
{
    int i;
    fin>>n;
    for(i=1; i<=n; i++)
        fin>>v[i];
    fin.close();
}
void dinamica()
{
    int i, j;
    lmax=1;
    for(i=n; i>=1; i--)
    {
        best[i]=1;
        nxt[i]=0;
        for(j=i+1; j<=n; j++)
            if(v[i]<v[j])
                if(best[i]<best[j]+1)
                {
                    best[i]=1+best[j];
                    nxt[i]=j;
                    if(best[i]>lmax)
                    {
                        lmax=best[i];
                        first=i;
                    }
                }
    }
}
void afisare()
{
    int i;
    fout<<lmax<<'\n';
    for(i=first; i!=0; i=nxt[i])
        fout<<v[i]<<' ';
    fout<<'\n';
    fout.close();
}
int main()
{
    citire();
    dinamica();
    afisare();
    return 0;
}