Cod sursa(job #1867047)

Utilizator AndreiITCuriman Andrei AndreiIT Data 3 februarie 2017 18:06:29
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>

using namespace std;

ifstream cin("scmax.in");
ofstream cout("scmax.out");

const int MAX = 100005;

int n, v[MAX], lung[MAX], pred[MAX];

void sir(int p){
    if(pred[p]!=0){
        sir(pred[p]);
    }
    cout<<v[p]<<' ';
}

int main()
{
    cin>>n;
    for(int i=1; i<=n; ++i)
        cin>>v[i];

    for(int i=1; i<=n; ++i){
        int lmax = 0;
        for(int j=1; j<i; ++j){
            if(v[j] < v[i])
                if(lung[j] > lmax){
                    lmax = lung[j];
                    pred[i] = j;
                }
        }
        lung[i] = 1 + lmax;
    }
    int pmax =1;
    for(int i=2; i<=n; ++i)
        if(lung[i] > lung[pmax]){
            pmax = i;
        }
    cout<<lung[pmax]<<'\n';
    sir(pmax);
    return 0;
}