Cod sursa(job #3033331)

Utilizator georgecristian2002Raducanu George-Cristian georgecristian2002 Data 23 martie 2023 19:12:29
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>

using namespace std;

#define MAXN 100007

#pragma GCC optimize("O3,unroll-loops")

inline int maxi(int a, int b) {
    if (a > b) return a;
    return b;
}

int main(void) {
    ifstream fin("scmax.in");
    ofstream fout("scmax.out");
    int n;
    int v[MAXN], d[MAXN], prev[MAXN];
    fin >> n;
    for (int i = 1; i <= n; i++) {
        fin >> v[i];
    }
    prev[1] = 0;
    d[1] = 1;

    for (int i = 2; i <= n; ++i) {
        d[i] = 1;
        for (int j = i - 1; j >= 1; --j) {
            if (v[i] > v[j]) {
                if (d[i] < d[j] + 1) {
                    d[i] = d[j] + 1;
                    prev[i] = j;
                }
            }
        }
    }
    int max = 0, poz = -1;
    for (int i = 1; i <= n; ++i) {
        if (d[i] > max) {
            max = d[i];
            poz = i;
        }
    }
    fout << max << "\n";
    // for (int i = 1; i <= n; ++i) {
    //     fout << d[i] << " ";
    // }
    // fout << "\n";
    // for (int i = 1; i <= n; ++i) {
    //     fout << prev[i] << " ";
    // }
    // fout << "\n";
    // search for the sequence
    // from the end to the beginning
    int seq[MAXN], k = 0;
    while (poz != 0) {
        //fout << v[poz] << " ";
        seq[k++] = v[poz];
        poz = prev[poz];
    }
    while (k > 0) {
        fout << seq[--k] << " ";
    }

    
    fin.close();
    fout.close();
    return 0;
}