Cod sursa(job #3240864)

Utilizator XxThornadoxXStoica Teodora XxThornadoxX Data 22 august 2024 10:00:23
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int a[1000005], b[1000005], l[1000005], poz, sol[1000005];

int cautbin(int st, int dr, int x)
{
    int aux = dr;
    while(st <= dr)
    {
        int mij = (st+dr)/2;
        if(a[l[mij+1]] >= x && a[l[mij]] < x)
            return mij;
        else if(x > a[l[mij]])
            st = mij+1;
        else
            dr = mij-1;
    }

    return aux;
}

int main()
{
    int n, ind = 0, cnt = 0;

    fin >> n;

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

    ind++;
    l[ind] = 1;

    for(int i = 2; i <= n; i++)
    {
        int j = cautbin(0, ind, a[i]);
        b[i] = l[j];
        l[j+1] = i;

        if(j+1 > ind)
        {
            poz = l[j+1];
            ind = j+1;
        }
    }

    fout << ind << "\n";

    while(ind--)
    {
        cnt++;
        sol[cnt] = a[poz];
        poz = b[poz];
    }

    if(cnt == 1)
        fout << a[1];
    else
        for(int i = cnt; i >= 1; i--)
            fout << sol[i] << " ";
}