Cod sursa(job #2674666)

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

using namespace std;

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

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

/// 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]<q)
            st=mij+1;
        else
        {
            dr=mij-1;
            rez=mij;
        }
    }
    return rez;
}

int main()
{
    fin >> n;
    int x;
    fin >> v[1];
    SCLM.push_back(v[1]);
    where[1]=1;
    lmax=1;
    for(int i=2;i<=n;i++)
    {
        fin >> v[i];
        int poz=CB(v[i]);
        where[i]=poz+1;
        if(poz==0)
        {
            SCLM[0]=v[i];
            continue;
        }
        if(poz==lmax)
        {
            lmax++;
            SCLM.push_back(v[i]);
            continue;
        }
        SCLM[poz]=v[i];
    }
    fout << lmax << '\n';
    int cont=lmax;
    int i=n;
    stack <int> ans;
    while(cont!=0)
    {
        if(where[i]==cont)
        {
            ans.push(v[i]);
            cont--;
        }
        i--;
    }
    while(!ans.empty())
    {
        fout << ans.top() << ' ';
        ans.pop();
    }
    return 0;
}