Cod sursa(job #3371238)

Utilizator agent_zebra34Dimitri agent_zebra34 Data 29 octombrie 2026 13:56:56
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Nmax = 1e5 + 3;

int n, len;
int a[Nmax], best[Nmax];

int id[Nmax], fromId[Nmax];

int CB(int x)
{
    int st = 1, dr = len, poz = len + 1;

    while(st <= dr)
    {
        int mij = (st + dr) / 2;

        if(best[mij] >= x)
        {
            poz = mij;
            dr = mij - 1;
        }
        else
            st = mij + 1;
    }

    return poz;
}

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

    for(int i = 1; i <= n; ++i)
    {
        int poz = CB(a[i]);

        best[poz] = a[i];
        id[poz] = i;
        if(poz > 1)
            fromId[i] = id[poz - 1];

        if(poz > len)
            len = poz;
    }

    fout << len << '\n';

    vector<int> sol;
    int idxRebuild = id[len];

    while(idxRebuild != 0)
    {
        sol.push_back(a[idxRebuild]);

        idxRebuild = fromId[idxRebuild];
    }

     for(int i = (int)sol.size() - 1; i >= 0; --i)
        fout << sol[i] << ' ';

    return 0;
}