Cod sursa(job #3375670)

Utilizator gogomanusAlbu Eduard Tudor gogomanus Data 12 noiembrie 2026 21:30:51
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
#define NMAX 100000

using namespace std;

ifstream fin("scmax.in")
ofstream fout("scmax.out");
int v[NMAX + 1], s[NMAX + 1] = {1};

int main(){
    int n; fin >> n;
    for(int i = 0; i < n; i++){
        fin >> v[i];
    }
    for (int l = 0; l < n; l++)
    {
        s[l] = 1;
    }
    for(int i = 1; i < n; i++){
        for(int j = 0; j < i; j++){
            if(v[j] < v[i]){
                s[i] = max(s[i], s[j] + 1);
            }
        }
    }
    int valMax = 0, pos = 0, numere[NMAX + 2];
    for(int i = 0; i < n; i++){
        if(s[i] >= valMax){
            valMax = s[i];
            pos = i;
        }
    }
    numere[valMax] = pos;
    fout<< valMax<< "\n";
    while(s[pos] > 1){
        for(int i = pos - 1; i >= 0; i--){
            if(s[i] == s[pos] - 1){
                numere[s[i]] = i;
                pos = i;
            }
        }
    }
    for(int k = 1; k <= valMax; k++){
        fout<< v[numere[k]]<< " ";
    }
    return 0;
}