Cod sursa(job #2701829)

Utilizator AndreibatmanAndrei Croitoriu Andreibatman Data 1 februarie 2021 21:13:10
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("scmax.in");
ofstream fout("scmax.out");
int a[100010],maxi,n,i,j,best[100010],poz[100010],p;
void dp()
{
    best[n]=1;
    poz[n]=-1;
    maxi=0;
    for(i=n-1;i>=1;i--)
    {
        best[i]=1;
        poz[i]=-1;
        for(j=i+1;j<=n;j++)
            if(a[i]<a[j] && best[i]<best[j]+1)
            {
                best[i]=best[j]+1;
                poz[i]=j;
                if(best[i]>maxi)
                    maxi=best[i],p=i;
            }
    }
}
int main()
{
    fin>>n;
    for(i=1;i<=n;i++)
        fin>>a[i];
    dp();
    fout<<maxi<<'\n';
    while(p!=-1)
    {
        fout<<a[p]<<" ";
        p=poz[p];
    }
    return 0;
}