Cod sursa(job #1229236)

Utilizator cdascaluDascalu Cristian cdascalu Data 16 septembrie 2014 19:36:28
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <stack>
#define Nmax 100001
using namespace std;

int N, vec[Nmax], best[Nmax], T[Nmax], sol, sol_pos;
void read()
{
    ifstream f("scmax.in");
    f>>N;
    for(int i=1;i<=N;++i)
        f>>vec[i];
    f.close();
}
void dinamica()
{
    //best[i] the longest sequence that starts with vec[i]

    fill(best, best + N + 1, 1);
    fill(T, T + N + 1, 0);

    sol = -1;
    for(int i=1;i<=N;++i)
    {
        for(int j=1;j<i;++j)
        {
            if(vec[j] < vec[i])
            {
                if(best[j] + 1 > best[i])
                {
                    best[i] = best[j] + 1;
                    T[i] = j;
                }
            }
        }
        if(best[i] > sol)
        {
            sol = best[i];
            sol_pos = i;
        }
    }
}
void write()
{
    ofstream g("scmax.out");
    g<<sol<<"\n";
    stack<int> Q;
    while(sol_pos != 0)
    {
        Q.push(vec[sol_pos]);
        sol_pos = T[sol_pos];
    }

    while(!Q.empty())
    {
        g<<Q.top()<<" ";
        Q.pop();
    }
    g.close();
}
int main()
{
    read();
    dinamica();
    write();
    return 0;
}