Cod sursa(job #3354902)

Utilizator StefanIancu2Stefan Iancu StefanIancu2 Data 21 mai 2026 09:38:11
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

int v[100001];
int dp[100001];

vector<int> rez;

int main()
{
    int n;
    fin >> n;

    for (int i = 1; i <= n; i++) {
        fin >> v[i];
    }

    for (int i = 1; i <= n; i++) {
        int maxi = 0;

        for (int j = 1; j < i; j++) {
            if (v[i] > v[j])
                maxi = max(maxi, dp[j]);
        }

        dp[i] = maxi + 1;
    }

    int rasp = 0, poz = 0;

    for (int i = 1; i <= n; i++) {
        if (dp[i] > rasp) {
            rasp = dp[i];
            poz = i;
        }
    }

    fout << rasp << '\n';

    rez.push_back(v[poz]);

    while (dp[poz] > 1) {
        for (int i = poz - 1; i >= 1; i--) {
            if (v[i] < v[poz] && dp[i] == dp[poz] - 1) {
                rez.push_back(v[i]);
                poz = i;
                break;
            }
        }
    }

    for (int i = rez.size() - 1; i >= 0; i--) {
        fout << rez[i] << " ";
    }

    return 0;
}