Cod sursa(job #2461218)

Utilizator urweakurweak urweak Data 25 septembrie 2019 09:20:36
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
#define nmax 100001
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");

int n, best[nmax], poz[nmax], maxim, p, a[nmax];

void read(){
    in >> n;
    for(int i = 1; i<=n; i++)
        in >> a[i];
}

void solve(){
    maxim = -1;
    best[n] = 1;
    poz[n] = -1;
    for(int i = n - 1; i>=1; i--){
        best[i] = 1;
        poz[i] = -1;
        for(int 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(maxim < best[i]) maxim = best[i], p = i;
        }
    }
}

void constr(){
    while(p!=-1){
        out << a[p] <<' ';
        p = poz[p];
    }
}

int main(){
    read();
    solve();
    out << best[p] <<"\n";
    constr();
    return 0;
}