Cod sursa(job #3005015)

Utilizator matei.tudoseMatei Tudose matei.tudose Data 16 martie 2023 18:53:06
Problema Subsir crescator maximal Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 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;
        }
        else
        {
            int poz = lower_bound(dp + 1, dp + 1 + ans, curent) - dp;
            dp[poz] = curent;
            indexCapatSir[poz] = i;
        }
    }
    fout << ans << "\n";
    for (int i = 1; i <= ans; i++)
        fout << values[indexCapatSir[i]] << " ";
    fout << "\n";
    return 0;
}