Cod sursa(job #3005020)

Utilizator matei.tudoseMatei Tudose matei.tudose Data 16 martie 2023 18:54:35
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>
#include <algorithm>
#include <stack>
using namespace std;

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

const int NMAX = 100000 + 5;
int n, dp[NMAX], indexCapatSir[NMAX], before[NMAX], values[NMAX];

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> values[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int curent = values[i];
        if (dp[ans] < curent)
        {
            dp[++ans] = curent;
            indexCapatSir[ans] = i;
            before[i] = indexCapatSir[ans - 1];
        }
        else
        {
            int poz = lower_bound(dp + 1, dp + 1 + ans, curent) - dp;
            dp[poz] = curent;
            indexCapatSir[poz] = i;
            before[i] = indexCapatSir[poz - 1];
        }
    }
    fout << ans << "\n";
    stack<int> scmax;
    int index_curent = indexCapatSir[ans];
    while (index_curent != 0)
    {
        scmax.push(values[index_curent]);
        index_curent = before[index_curent];
    }
    while (!scmax.empty())
    {
        fout << scmax.top() << " ";
        scmax.pop();
    }
    fout << "\n";
    return 0;
}