Cod sursa(job #1784953)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 20 octombrie 2016 18:12:16
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <iostream>
#include <fstream>
#include <stack>

using namespace std;

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

unsigned int v[100001];
int n;
int s[100001];
int ant[100001];

const unsigned int INF = (1 << 31);

void citire()
{
    in >> n;
    for(int i = 1; i <= n; ++i)
        in >> v[i];
}

void rezolvare()
{
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j < i; ++j)
        {
            if(s[j] >= s[i] && v[j] < v[i])
            {
                s[i] = s[j] + 1;
                ant[i] = j;
            }
        }
    }
    int mx = -INF;
    int sol;
    for(int i = 1; i <= n; ++i)
    {
        if(s[i] > mx)
        {
            mx = s[i];
            sol = i;
        }
    }
    out << mx << "\n";
    stack<unsigned int> ras;
    for(int i = sol; i > 0; i = ant[i])
        ras.push(v[i]);
    while(ras.empty() == false)
    {
        out << ras.top() << " ";
        ras.pop();
    }
}

int main()
{
    citire();
    rezolvare();
}