Cod sursa(job #3031188)

Utilizator preda.andreiPreda Andrei preda.andrei Data 18 martie 2023 22:50:14
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

vector<int> Solve(const vector<int>& vec) {
    vector<int> dp(vec.size(), 1);
    vector<int> prev(vec.size(), -1);

    for (size_t i = 0; i < vec.size(); i += 1) {
        for (size_t j = 0; j < i; j += 1) {
            if (vec[j] < vec[i] && dp[j] + 1 > dp[i]) {
                dp[i] = dp[j] + 1;
                prev[i] = j;
            }
        }
    }

    int pos = 0;
    for (size_t i = 0; i < vec.size(); i += 1) {
        if (dp[i] > dp[pos]) {
            pos = i;
        }
    }

    vector<int> sub;
    while (pos >= 0) {
        sub.push_back(vec[pos]);
        pos = prev[pos];
    }
    reverse(sub.begin(), sub.end());
    return sub;
}

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

    int n;
    fin >> n;

    vector<int> vec(n);
    for (auto& num : vec) {
        fin >> num;
    }

    auto res = Solve(vec);
    fout << res.size() << "\n";
    for (const auto& num : res) {
        fout << num << " ";
    }
    fout << "\n";
    return 0;
}