Cod sursa(job #1836287)

Utilizator hantoniusStan Antoniu hantonius Data 28 decembrie 2016 10:16:49
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#define maxn 100005
using namespace std;

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

int v[maxn], best[maxn], prec[maxn], n, ras = 1, poz = 1;

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

void solve()
{
    int ok;

    best[1] = 1;
    for (int i = 2; i <= n; i++) {
        ok = 0;
        for (int j = 1; j < i; j++)
            if (best[j] + 1 > best[i] && v[j] < v[i]) {
                best[i] = best[j] + 1;
                prec[i] = j;
                ok = 1;
            }
        if (ok == 0) {
            best[i] = 1;
        }
        if (best[i] > ras) {
            ras = best[i];
            poz= i;
        }
    }
}

void afis(int x)
{
    if (x != 0) {
        afis(prec[x]);
        fout << v[x] << ' ';
    }
}

void write()
{
    fout << ras << '\n';
    afis(poz);
}

int main()
{
    read();
    solve();
    write();
    return 0;
}