Cod sursa(job #2779284)

Utilizator VladutzPredoiVlad Predoi VladutzPredoi Data 3 octombrie 2021 10:54:01
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>
using namespace std;


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


int n,maxt,maxx,pm,cnt;
int A[100005];
int L[100005];
int afs[100005];

void afis(int k, int m)
{
    int i=k-1;
    if(m>0)
    {
        int i=k-1;
        while(L[i]!=m-1 || A[i]>=A[k])
            i--;
        afis(i,m-1);
        afs[m]=A[k];
    }
}


void read()
{
    fin>>n;
    for(int i=1; i<=n; i++)
    {
        fin>>A[i];

    }
}


void dinamic()
{
    L[1]=1;
    for(int i=2; i<=n; i++)
    {
        maxx=0;
        for(int j=1; j<i; j++)
            if(A[j]<A[i] && L[j]>maxx) maxx=L[j];
        L[i]=maxx+1;
        if(L[i]>maxt)
        {
            maxt=L[i];
            pm=i;
        }
    }
    afis(pm,maxt);
    fout<<maxt<<"\n";
    for(int i=1; i<=maxt; i++)
    {
        fout<<afs[i]<<" ";
    }
}


int main()
{
    read();
    dinamic();
    return 0;
}