Cod sursa(job #1294419)

Utilizator apostolandreiApostol Andrei Laurentiu apostolandrei Data 17 decembrie 2014 15:43:43
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
using namespace std;
#define nmax 10009

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

int lung[nmax], v[nmax], pred[nmax], i, j, lmax = 0;

void subsir (int n) {
    lung[1] = 1;
    for (i = 2; i <= n; i++) {
        lmax = 0;
        for (j = 1; j < i; j++)
            if (v[j] < v[i])
                if (lung[j] > lmax) {
                    lmax = lung[j];
                    pred[i] = j;
                }
        lung[i] = 1 + lmax;
    }
}

void reconstruire (int p) {
    if (pred[p] != 0)
        reconstruire(pred[p]);
    out << v[p] << " ";
}


int main() {


    int n, maxim = 0, poz = 1;
    in >> n;
    for (i = 1; i <= n; i++) in >> v[i];

    lung[1] = 1;
    subsir(n);
    for (i = 1; i <= n; i++){
        if (lung[i] > maxim) {
            maxim = lung[i];
            poz = i;
        }

    }

    out << lung[poz] << endl;

    reconstruire(poz);

    return 0;
}