Cod sursa(job #1681305)

Utilizator Y0da1NUME JMECHER Y0da1 Data 9 aprilie 2016 13:01:01
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
#include <iostream>
using namespace std;
int n, a[100002], best[100002], pos[100002];
int main()
{
    int i, j, maxim=1, p;
    ifstream g ("scmax.in");
    ofstream h ("scmax.out");
    g>>n;
    for(i=1;i<=n;i++)
        g>>a[i];
    //dinamic
    best[n]=1;
    pos[n]=-1;
    for(i=n-1;i>0;--i)
    {
        best[i]=1;
        pos[i]=-1;
        for(j=i+1;j<=n;j++)
            if(a[i]<a[j] && best[i]<best[j]+1)
            {
            best[i]=best[j]+1;
            pos[i]=j;
            if(best[i]>maxim)
                {
                    maxim=best[i];p=i;
                }
            }
    }
    i=p;
    h<<maxim<<"\n";
    while(i!=-1)
    {
    h<< a[i]<<" ";
    i=pos[i];
    }
    g.close();
    h.close();
    return 0;
}