Cod sursa(job #2639912)

Utilizator Ionut_neuer58Raducu Ioan Stefan Ionut_neuer58 Data 4 august 2020 14:27:13
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int v[100001], p[100001], s[100001], best[100001];
int n, ns=1, poz;

int bins(int x)
{
    int i, step=1;
    while(step<ns) step<<=1;
    for(i=0; step; step>>=1)
        if(step+i<=ns && v[s[step+i]]<x)
            i+=step;
    return i;
}

int main()
{
    in>>n;
    s[1]=best[1]=1;
    for(int i=1; i<=n; i++) in>>v[i];
    for(int i=2; i<=n; i++)
    {
        poz=bins(v[i]);
        p[i]=s[poz];
        best[i]=poz+1;
        s[poz+1]=i;
        if(poz+1>ns) ns=poz+1;
    }
    int ind=1;
    for(int i=2; i<=n; i++)
        if(best[i]>best[ind]) ind=i;
    ns=1;
    out<<best[ind]<<'\n';
    while(ind) s[ns++]=v[ind], ind=p[ind];
    for(int i=ns-1; i; i--) out<<s[i]<<' ';
    return 0;
}