Cod sursa(job #2572819)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 5 martie 2020 14:26:08
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>

using namespace std;

void usain_bolt()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
}

const int N = 1e5 + 5;

int a[N], dad[N], f[N];

int bs(int x)
{
    int left = 1, right = f[N - 1], mx = 0;
    while(left <= right) {
        int mid = (left + right) >> 1;
        if(x > a[f[mid]]) mx = max(mx, mid), left = mid + 1;
        else right = mid - 1;
    }
    return mx;
}

void path(int k)
{
    if(dad[k] == 0) cout << a[k] << " ";
    else {
        path(dad[k]);
        cout << a[k] << " ";
    }
}
int main()
{
    usain_bolt();

    int n;

    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> a[i];
    f[++f[N - 1]] = 1;
    for(int i = 2; i <= n; ++i) {
        int pos = bs(a[i]);
        dad[i] = f[pos];
        f[pos + 1] = i;
        f[N - 1] = max(f[N - 1], pos + 1);
    }
    cout << f[N - 1] << "\n";
    path(f[f[N - 1]]);
    return 0;
}