Cod sursa(job #2763642)

Utilizator AlexZeuVasile Alexandru AlexZeu Data 15 iulie 2021 17:07:54
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h>
#define ll long long
#define nl '\n'
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define F0R(i, a, b) for (int i = a; i >= b; --i)
#define FORd(i, n) for (int i = 0; i < n; ++i)
#define F0Rd(i, n) for (int i = n - 1; i >= 0; --i)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN = 1e5 + 5;

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

int N, a[mxN], dp[mxN], fiu[mxN];

void solve() {
    fin >> N;
    for (int i = 1; i <= N; ++i) {
        fin >> a[i];
        dp[i] = 1;
    }
    int ans = 1;
    for (int i = 2; i <= N; ++i) {
        for (int j = 1; j <= i; ++j) {
            if (a[i] > a[j] && dp[j] + 1 > dp[i]) {
                dp[i] = dp[j] + 1;
                fiu[i] = j;
            }
        }
        if (dp[ans] < dp[i]) {
            ans = i;
        }
    }
    stack<int> rez;
    while (ans != 0) {
        rez.push(a[ans]);
        ans = fiu[ans];
    }
    fout << rez.size() << nl;
    while (rez.size()) {
        fout << rez.top() << " ";
        rez.pop();
    }
}

int main() {
    fin.tie(0); fout.tie(0);
    ios::sync_with_stdio(0);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

//read the question correctly (ll vs int)
//what's the meaning of the problem ? Think outside the BOX !!!
//edge cases ?
//make it simple
//write everything (observations, edge cases, ideas, steps, methods, ...)