Cod sursa(job #3341978)

Utilizator gigiduru9088Gigel Duru gigiduru9088 Data 22 februarie 2026 11:43:45
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

long long a[100001], dp[100001], last[100001], poz_dp[100001];
int n;
int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> a[i];
    int lungime_max = 0;
    for(int i = 1; i <= n; i++)
    {
        int st = 1, dr = lungime_max, poz = 0;
        while (st <= dr)
        {
            int mid = st + (dr - st) / 2;
            if (dp[mid] < a[i]) {
                poz = mid;
                st = mid + 1;
            } else {
                dr = mid - 1;
            }
        }
        int l_actual = poz + 1;
        dp[l_actual] = a[i];
        poz_dp[l_actual] = i;
        last[i] = poz_dp[poz];

        if(l_actual > lungime_max)
        {
            lungime_max = l_actual;
        }
    }
        fout << lungime_max << endl;

    vector <int> solutie;
    int k = poz_dp[lungime_max];
    while(k != 0)
    {
        solutie.push_back(a[k]);
        k = last[k];
    }
    for(int i = solutie.size() - 1; i >= 0; i--)
    {
        fout << solutie[i] << ' ';
    }
    return 0;
}