Cod sursa(job #2674650)

Utilizator csamoilasamoila ciprian casian csamoila Data 19 noiembrie 2020 19:21:15
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int> SCLM[100002]; /// SCLM[i]=sirul cresc de lung i+1 cel mai optim, 0 <= i <n
int lmax,n;

/// returneaza poz primului elem mai mare ca q
int CB(int q)
{
    int st=0,dr=lmax-1;
    int rez=lmax;
    while(st<=dr)
    {
        int mij=(st+dr)/2;
        if(SCLM[mij][mij]<q)
            st=mij+1;
        else
        {
            dr=mij-1;
            rez=mij;
        }
    }
    return rez;
}

int main()
{
    fin >> n;
    int x;
    fin >> x;
    SCLM[0].push_back(x);
    lmax=1;
    for(int i=2;i<=n;i++)
    {
        fin >> x;
        int poz=CB(x);
        //cout << x << ' ' << poz << '\n';
        if(poz==0)
        {
            SCLM[0][0]=x;
            continue;
        }
        if(poz==lmax)
            lmax++;
        SCLM[poz].clear();
        for(int j=0;j<=poz-1;j++)
            SCLM[poz].push_back(SCLM[poz-1][j]);
        SCLM[poz].push_back(x);
    }
    fout << lmax << '\n';
    for(int i=0;i<lmax;i++) fout << SCLM[lmax-1][i] << ' ';
    return 0;
}