Cod sursa(job #2682173)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 7 decembrie 2020 22:43:37
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
using namespace std;

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

int n;
int a[100005],lis[100005],ord[100005],ind[100005],k;
int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> a[i];
    lis[++k] = a[1];
    ord[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        if (a[i] > lis[k])lis[++k] = a[i], ord[i] = k;
        else {
            int poz = lower_bound(lis + 1, lis + k + 1,a[i]) - lis;
            lis[poz] = a[i];
            ord[i] = poz;
        }
   }
    fout << k << "\n";
    int j = n;
    for (int i = k; i >= 1; i--)
    {
        while (ord[j] != i)
            j--;
        ind[i] = j;
    }
    for (int i = 1; i <= k; i++)
        fout << a[ind[i]] << " ";
   
    fin.close();
    fout.close();
    return 0;
}