Cod sursa(job #1848022)

Utilizator KavarnaFlorin Salam Kavarna Data 15 ianuarie 2017 13:01:32
Problema Subsir crescator maximal Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <vector>
#include <fstream>

std::ifstream ifCitire("scmax.in");
std::ofstream ofAfisare("scmax.out");

void CalculateLIS(std::vector< int > &D)
{
    int max = 1,index = 0;
    std::vector< std::vector<int> > L(D.size());
    L[0].push_back(D[0]); // The longest increasing subsequence up untill this point is D[0]

    for (unsigned int i = 1; i < D.size(); ++i) // Iterate through the remaining vector
    {
        for (unsigned int j = 0; j < i; ++j)
            if (D[i] > D[j] && L[i].size() < L[j].size() + 1) // Get the longest subsequence from the 'past' with the last element less than the actual element
                L[i] = L[j]; // Assign the longest subsequence to this subsequence
        L[i].push_back(D[i]); // Append the last element
        if (L[i].size() > max)
        {
            max = L[i].size();
            index = i;
        }
    }
    ofAfisare << max << "\n";
    for (auto & iter : L[index])
        ofAfisare << iter << ' ';

}

int main()
{
    int N,val;
    std::vector<int> vec;
    ifCitire >> N;
    for (int i = 0; i < N; ++i)
    {
        ifCitire >> val;
        vec.push_back(val);
    }
    CalculateLIS(vec);
    ifCitire.close();
    ofAfisare.close();
    return 0;
}