Cod sursa(job #3213053)

Utilizator maiaauUngureanu Maia maiaau Data 12 martie 2024 13:49:26
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back

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

const int N = 1e5+3;

int n, l, v[N], prv[N], idx[N];
vector<int> dp;

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++) {
        fin >> v[i];
        auto it = lower_bound(dp.begin(), dp.end(), v[i]);
        int pos = (int)(it - dp.begin());
        if (it == dp.end())
            dp.pb(v[i]), l++;
        else
            dp[pos] = v[i];   
         
        idx[pos] = i;
        prv[i] = idx[pos-1]; 
    }
    fout << l << '\n';

    stack<int> rez; 
    int which = idx[l-1];
    for (; l; l--){
        rez.push(v[which]);
        which = prv[which];
    }

    while (!rez.empty()){
        fout << rez.top() << ' ';
        rez.pop();
    }
    
    return 0;
}