Cod sursa(job #3129422)

Utilizator ADelegeanuAlex Delegeanu ADelegeanu Data 14 mai 2023 14:50:41
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <bits/stdc++.h>

void printVect(const std::vector<int>& v) {
    for (auto x : v) {
        std::cout << x << " ";
    }
    std::cout << std::endl;
}

std::vector<int> bestSeq(const std::vector<int>& nums) {
    // fill best
    std::vector<int> best(nums.size());

    best[0] = 1;
    for (auto i = 1u; i < nums.size(); i++) {
        int currentBest = 0;
        for (int j = i - 1; j >= 0; --j) {
            if (nums[i] > nums[j] && best[j] > currentBest) {
                currentBest = best[j];
            }
        }
        best[i] = currentBest + 1;
    }

    // find best length
    int bestLength = *std::max_element(best.begin(), best.end());

    // find the position where best length is
    int bestPos = nums.size() - 1;
    while (bestPos > 0) {
        if (best[bestPos] == bestLength)
            break;
        bestPos--;
    }

    // fill the result
    std::vector<int> res(bestLength);
    for (int i = bestPos, idx = bestLength - 1; i >= 0; --i) {
        if (best[i] == bestLength) {
            res[idx--] = nums[i];
            bestLength--;
        }
    }

    return res;
}

int main() {
    std::ifstream iStream("scmax.in");
    int size{ 0 };
    iStream >> size;
    std::vector<int> nums(size);
    for (int i = 0; i < size; i++)
        iStream >> nums[i];
    iStream.close();

    std::ofstream oStream("scmax.out");
    auto res = bestSeq(nums);
    oStream << res.size() << '\n';
    for (auto val : res)
        oStream << val << ' ';
    oStream.close();

    return 0;
}