Cod sursa(job #3320289)

Utilizator pachy2007Pachitanu Matei pachy2007 Data 4 noiembrie 2025 20:12:21
Problema Subsir crescator maximal Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>

#define ll long long
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");

ll n,sol,dp[100002],lungime[100002], v[100002],lung=0, solutie[100002], precedent=1e16;

void place(ll x, ll i)
{
    if(lung==0)
    {
        lung=1;
        dp[lung]=1;
        lungime[1]++;
        sol=1;
        return;
    }

    ll st=1;
    ll dr=lung;
    while(st<=dr)
    {
        ll mij=(st+dr)/2;
        if(x>v[dp[mij]])dr=mij-1;
        else st=mij+1;

    }
    if(st>lung)lung++;
    lungime[i]=lungime[dp[st]]+1;
    dp[st]=i;
    if(lungime[i]>sol)sol=lungime[i];
}
ll x;
int main()
{
    fin>>n;
    for(ll i=1; i<=n; i++)
    {
        fin>>v[i];
        place(v[i], i);
    }
    fout<<sol<<'\n';
    ll k=0;
    for(ll i=n; i>=1; i--)
    {
        if(sol-k==lungime[i] && v[i]<precedent)
        {
            solutie[sol-k]=v[i];
            precedent=v[i];
            k++;
        }
        if(sol-k==0)break;
    }


    for(ll i=1;i<=sol;i++)
    fout<<solutie[i]<<' ';
    return 0;
}