Cod sursa(job #3304639)

Utilizator Gabriel_DaescuDaescu Gabriel Florin Gabriel_Daescu Data 25 iulie 2025 17:00:04
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>
#define NMAX 100002
using namespace std;
ifstream  fin("scmax.in");
ofstream fout("scmax.out");
int N,a[NMAX],dp[NMAX],f[NMAX];

void citire()
{
    fin>>N;

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

int main()
{
    citire();

    for(int i=N; i>=1; i--)
    {
        int lmax,poz;
        lmax=poz=0;

        for(int j=i+1; j<=N; j++)
        {
            if(a[i]<a[j] && dp[j]>lmax)
            {
                lmax=dp[j];
                poz=j;
            }
        }

        dp[i]=1+lmax;
        f[i]=poz;
    }

    int ans,p;
    ans=p=0;
    for(int i=1; i<=N; i++)
    {
        if(dp[i]>ans)
        {
            ans=dp[i];
            p=i;
        }
    }

    fout<< ans << "\n";

    for(int i=p; i; i=f[i])
    {
        fout<< a[i] << " ";
    }
    fout<< "\n";

    return 0;
}