Cod sursa(job #2936907)

Utilizator C_R_I_S_T_I_2_3Cristi Gavrila C_R_I_S_T_I_2_3 Data 9 noiembrie 2022 17:49:22
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, cnt;
int v[100005], lis[100005], poz[100005];
stack <int> solutie;

int CB(int x)
{
    int st = 1, dr = cnt, mij, sol;
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        if(x > v[mij])
        {
            st = mij + 1;
        }
        else
        {
            dr = mij - 1;
            sol = mij;
        }
    }
    return sol;
}
int main()
{
    fin >> n;
    for(int i = 1; i <= n; i ++)
    {
        fin >> v[i];
    }

    poz[1] = 1;
    lis[1] = v[1];
    cnt = 1;
    for(int i = 2; i <= n; i ++)
    {
        if(v[i] > lis[cnt])
        {
            lis[++cnt] = v[i];
            poz[i] = cnt;
        }
        else
        {
            int aux = CB(v[i]);
            lis[aux] = v[i];
            poz[i] = aux;
        }
    }
    
    fout << cnt << "\n";

    for(int i = n; i >= 1; i --)
    {
        if(poz[i] == cnt)
        {
            solutie.push(v[i]);
            cnt--;
        }
    }
    while(!solutie.empty())
    {
        fout << solutie.top() << " ";
        solutie.pop();
    }
    return 0;
}