Cod sursa(job #2158204)

Utilizator TMogaTudor Moga TMoga Data 10 martie 2018 11:17:17
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int main()
{
    int n;
    in >> n;
    int ind[n], t[n], last[n], k = -1, a[n];

    for (int i = 0; i < n; i++)
    {
        in >> a[i];
    }

    k++;
    last[k] = a[0];
    ind[k] = 0;
    t[0] = -1;

    for (int i = 1; i < n; i++)
    {
        if (a[i] > last[k])
        {
            k++;
            last[k] = a[i];
            ind[k] = i;
            t[i] = ind[k-1];
        }
        else
        {
            int dr, st, m;
            dr = k;
            st = 0;
            while (st <= dr)
            {
                m = (dr - st)/2;
                if (last[m-1] < a[i] <= last[m])
                {
                    last[m] = a[i];
                    t[i] = t[ind[m]];
                    ind[m] = i;
                    break;
                }
                else if (a[i] < last[m])
                    dr = m;
                else if (a[i] > last[m])
                    st = m;
            }
        }
    }
    int o[k], x = 0;
    out << k + 1 <<endl;
    o[x++] = last[k];
    for (int i = 0; i < k; i++)
        o[x++] = a[t[ind[k-i]]];

    for (int i = k; i >= 0; i--)
        out << o[i] << ' ';
    return 0;
}