Cod sursa(job #2044145)

Utilizator razvan171514Razvan Mihai razvan171514 Data 20 octombrie 2017 22:36:44
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#define lim 100003
using namespace std;
ifstream f ("scmax.in");
ofstream g ("scmax.out");
int n,v[lim],best[lim], maxi,sol=0,poz[lim],p;
void read ()
{
    f>>n;
    for (int i=1;i<=n;++i)
        f>>v[i];
}
void formare ()
{
    best[n]=1;
    poz[n]=-1;
    maxi=1; p=n;
    for(int i=n-1;i>=1;--i)
    {
        best[i]=1;
        poz[i]=-1;
        for(int j=i+1;j<=n;++j)
            if(v[i]<v[j] && best[i]<best[j]+1)
            {
                best[i]=best[j]+1;
                poz[i]=j;
                if(best[i]>maxi)
                {
                    maxi=best[i];
                    p=i;
                }
            }
    }
    g<<maxi<<'\n';
}
void construct()
{
    int i;
    i=p;
    while(i!=-1)
    {
        g<<v[i]<<' ';
        i=poz[i];
    }
}
int main()
{
    read();
    formare();
    construct();
    return 0;
}