Cod sursa(job #3302364)

Utilizator brianabucur11Briana Bucur brianabucur11 Data 6 iulie 2025 23:46:25
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax=1e5+5;
const int inf=2e9+5;

int n, v[nmax], dp[nmax], ult[nmax];

int cb (int i)
{
    int st=1, dr=n, poz=0;
    while (st<=dr)
    {
        int mij=(st+dr)/2;
        if (v[i]>v[dp[mij]])
        {
            st=mij+1;
            poz=mij;
        }
        else
            dr=mij-1;
    }
    return poz;
}

void afis (int i)
{
    if (i==0)
        return;
    afis(ult[i]);
    fout << v[i] << " ";
}

void pd ()
{
    //dp[lg]=val min care poate fi pusa la finalul unui scmax de lungime lg
    v[0]=-inf;
    v[n+1]=inf;
    for (int i=1; i<=n; i++)
        dp[i]=n+1;
    int rez=0;
    for (int i=1; i<=n; i++)
    {
        int poz=cb(i)+1;
        dp[poz]=i;
        ult[i]=dp[poz-1];
        rez=max(rez,poz);
    }
    fout << rez << '\n';
    afis(dp[rez]);
}

signed main()
{
    fin >> n;
    for (int i=1; i<=n; i++)
        fin >> v[i];
    pd();
    return 0;
}