Cod sursa(job #1966230)

Utilizator nicu_serteSerte Nicu nicu_serte Data 15 aprilie 2017 00:12:14
Problema Subsir crescator maximal Scor 45
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 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, mx, poz=0;
    lmax=1;
    for(i=n; i>=1; i--)
    {
        best[i]=1;
        mx=0;
        for(j=i+1; j<=n; j++)
            if(v[i]<v[j])
                if(best[j]>mx)
                {
                    mx=best[j];
                    poz=j;
                }
        best[i]=1+mx;
        nxt[i]=poz;
        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;
}