Cod sursa(job #1024413)

Utilizator japjappedulapPotra Vlad japjappedulap Data 8 noiembrie 2013 18:03:14
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <stack>
using namespace std;

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

int n, a[100001], l[100001], vmax, r = 1;
stack <int> v;

int main()
{
    is >> n;
    for (int i = 0; i < n; ++i)
        is >> a[i];
    for (int i = 0; i < n; ++i)
    {
        vmax = 0;
        for (int j = i-1; j >= 0; j--)
        {
            if (a[i] > a[j] && vmax < a[j])
            {
                l[i] = l[j]+1;
                vmax = a[j];
            }
        }
        if (l[i] == 0) l[i] = 1;
        if (l[i] == r)
            r++;
    }
    r--;
    os << r << '\n';
    for (int i = n-1; i >= 0 && r != 0; i--)
        if (l[i] == r)
        {
            v.push(a[i]);
            r--;
        }
    while (!v.empty())
    {
        os << v.top() << ' ';
        v.pop();
    }
    is.close();
    os.close();
    return 0;
}