Cod sursa(job #2924225)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 27 septembrie 2022 16:33:53
Problema Subsir crescator maximal Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int>lis;
int d[100005],pre[100005],pos[100005];
int n,a[100005];

int main()
{
    in >> n;
    for (int i = 1; i <= n; i++)
        in >> a[i];
    for (int i = 1; i <= n; i++)
        d[i] = 1e9;
    for (int i = 1; i <= n; i++)
    {
        int st = 0,pas = 1 << 16;
        while (pas != 0)
        {
            if (st + pas <= n and d[st + pas] < a[i])
                st += pas;
            pas /= 2;
        }
        st++;
        d[st] = a[i];
        pos[st] = i;
        pre[i] = pos[st - 1];
    }
    int lst = 0;
    for (int i = 1; i <= n; i++)
        if (d[i] != 1e9)
            lst = i;
    lst = pos[lst];
    while (lst != 0)
        lis.push_back(a[lst]),lst = pre[lst];
    reverse(lis.begin(),lis.end());
    out << lis.size() << '\n';
    for (int i = 0; i < lis.size(); i++)
        out << lis[i] << ' ';
    return 0;
}