Cod sursa(job #1042394)

Utilizator lucianRRuscanu Lucian lucianR Data 26 noiembrie 2013 23:00:53
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>
#define N_MAX 100000
#define INF -2000000

using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");

int n, v[N_MAX], best[N_MAX], pre[N_MAX], strt;
int mx = best[0];

int v_max(int c)
{
    int vmax = INF, pmax = -1;
    for(int i = 0; i < c; i++)
        if(v[i] < v[c] && best[i] > vmax)
        {
            vmax = best[i];
            pmax = i;
        }
    if(pmax != -1) pre[c] = pmax;
    if(vmax == INF) return 0;
    return vmax;
}

void scm()
{
    best[0] = 1;

    for(int i = 1; i < n; i++)
    {
        best[i] = v_max(i) + 1;
        if(best[i] > mx)
        {
            mx = best[i];
            strt = i;
        }
    }
}

void afisare(int x)
{
    if(pre[x] != -1)
        afisare(pre[x]);
    out << v[x] << " ";
}

int main()
{
    in >> n;
    for(int i = 0; i < n; i++)
    {
        in >> v[i];
        pre[i] = -1;
    }
    scm();
    out << mx << '\n';
    afisare(strt);
    return 0;
}