Cod sursa(job #1550253)

Utilizator felipeGFilipGherman felipeG Data 13 decembrie 2015 14:21:47
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#define N 100010
using namespace std;

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

int n, maxi, poz, v[N], s[N], p[N];
bool ok;

void afis(int m, int i)
{
    if (i > 0)
    {
        if(p[i] == m)
        {
            afis (m - 1, i - 1);
            g << v[i] << " ";
        }
        else
            afis (m, i - 1);
    }
}

int main()
{
    f >> n;
    for (int i = 1; i <= n; ++i)
        f >> v[i];

    s[1] = v[1];
    p[1] = 1;
    maxi = 1;

    for (int i = 2; i <= n; ++i)
    {
        ok = poz = 0;
        for (int j = 1; j <= maxi && !ok; ++j)
            if (s[j] >= v[i])
            {
                ok = 1;
                poz = j;
            }
        if (ok)
        {
            s[poz] = v[i];
            p[i] = poz;
        }
        else
        {
            maxi++;
            s[maxi] = v[i];
            p[i] = maxi;
        }
    }
    g << maxi << endl;
    afis (maxi, n);
    return 0;
}