Cod sursa(job #3334069)

Utilizator Ionut2212Nedelcu Alexandru Ionut Ionut2212 Data 16 ianuarie 2026 09:25:10
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream fin ("scmax.in");
ofstream fout("scmax.out");
int v[100001], z, ans[100001], rec[100001], pstart, ans2[100001];
int main()
{
    int n;
    fin >> n;
    for(int i = 1; i<=n; i++)
    {
        fin >> v[i];
    }
    for(int i = 1; i<=n; i++)
    {
        int x = v[i], poz;
        if(z == 0)
        {
            ans[++z] = x;
            poz = 1;
        }
        else
        {
            poz = lower_bound(ans+1, ans+1+z, x) - ans;
            if(poz > z)
            {
                ans[++z] = x;
            }
            else
            {
                ans[poz] = x;
            }
        }
        rec[i] = poz;
    }
    int pozitie=z;
    fout << z << '\n';
    for(int i = n; i>0; i--)
    {
        if(rec[i] == z)
        {
            ans2[z] = v[i];
            z--;
        }
    }
    for(int i = 1; i<=pozitie; i++)
    {
        fout << ans2[i] << ' ';
    }
    return 0;
}