Cod sursa(job #2918132)

Utilizator Mihai_EduardMihai Eduard Mihai_Eduard Data 10 august 2022 02:34:14
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int NMAX=100005;

///dp[i]=the minimum ending value of an increasing subsequence of length i, using the elements considered so far
int N, v[NMAX], dp[NMAX], nr, L[NMAX], nxt=INT_MAX;

void afisare(int val, int poz){
    if(val==0)
        return;
    while(L[poz]!=val or v[poz]>=nxt)
        poz--;
    nxt=v[poz];
    afisare(val-1,poz-1);
    fout<<v[poz]<<' ';
}

int main()
{
    fin>>N;
    for(int i=1;i<=N;i++)
        fin>>v[i];

    dp[++nr]=v[1];
    L[1]=1;
    for(int i=2;i<=N;i++){
        int poz=lower_bound(dp+1,dp+nr+1,v[i])-dp;
        if(poz>nr)
            nr++;
        dp[poz]=v[i];
        L[i]=poz;
    }

    fout<<nr<<'\n';
    afisare(nr,N);

    fin.close();
    fout.close();
    return 0;
}