Cod sursa(job #2515018)

Utilizator As932Stanciu Andreea As932 Data 27 decembrie 2019 17:10:01
Problema Subsir crescator maximal Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");

int main()
{
    long long n;
    long long lmax=1,in=1;

    fin>>n;

    long long a[n+1];
    long long best[n+1];
    long long pre[n+1];

    for(long long i=1;i<=n;i++)
        fin>>a[i];

    best[1]=1;
    pre[1]=-1;

    for(long long i=2;i<=n;i++)
    {
        best[i]=1;
        pre[i]=-1;

        for(long long j=1;j<i;j++)
            if(a[j]<a[i] && best[i]<best[j]+1)
            {
                best[i]=best[j]+1;
                pre[i]=j;

                if(lmax<best[i])
                {
                    lmax=best[i];
                    in=n-lmax;
                }
            }
    }

    fout<<lmax<<"\n";

    fout<<a[in]<<" ";
    for(long long i=1;i<=n;i++)
        if(pre[i]!=-1 && pre[i]!=pre[i-1])
            fout<<a[i]<<" ";

    return 0;
}