Cod sursa(job #2573069)

Utilizator alex_bb8Banilean Alexandru-Ioan alex_bb8 Data 5 martie 2020 15:41:16
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, lgmax, ant;
int v[100003], lg[100003], ilt[100003];


void rec(int poz)
{
    for (int i = poz; i >= 1; --i)
    {
        if (lgmax == lg[i] && v[i] < ant)
        {
            --lgmax;
            ant = v[i];
            rec(i - 1);
            g<< v[i] << " ";
            return;
        }
    }
}

int main()
{
    f >> n;

    memset(ilt,INT_MAX,n+2);
    ilt[1] = INT_MIN;

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

        int s = 1, d = lgmax, m;

        while(s <= d)
        {
            m = (s + d)>>1;
            if (ilt[m] < v[i]) s = m + 1;
            else d = m - 1;
        }

        ilt[s] = v[i];
        lg[i] = s;
        if (lgmax < s) lgmax = s;

    }

    g << lgmax << "\n";

    ant = INT_MAX;

    rec(n);
    return 0;
}