Cod sursa(job #2765874)

Utilizator SerbaP123Popescu Serban SerbaP123 Data 30 iulie 2021 10:51:31
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
using namespace std;

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

const int nmax = 1e5;
int a[nmax + 1], x[nmax + 1], t[nmax + 1], n, p, u, m, s;
vector <int> ans;

void afis(int m){
    if(m){
        afis(t[m]);
        cout << a[m] << " ";
    }
}

int main(){
    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    x[1] = 1;
    p = 1, s = 1;
    for(int i = 2; i <= n; ++i){
        p = 1;
        u = s;
        while(p <= u){
            m = (p + u) / 2;
            if(a[i] > a[x[m]]){
                p = m + 1;
            }
            else{
                u = m - 1;
            }
        }
        if(p > s){
            s++;
        }
        x[p] = i;
        t[i] = x[p - 1];
    }
    cout << s << "\n";
    afis(x[s]);
    return 0;
}