Cod sursa(job #1006154)

Utilizator PsychoAlexAlexandru Buicescu PsychoAlex Data 6 octombrie 2013 15:24:38
Problema Subsir crescator maximal Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 2.75 kb
#include <iostream>
#include <fstream>

std::ifstream fin("scmax.in");
std::ofstream fout("scmax.out");

struct nod
{
    int val;
    nod *st, *dr;
};
nod *varf = new nod;
//long n, a[100000], l[100000];

int n, maxL;

void insereaza(nod *p, int val)
{
//    if(p == NULL)
//    {
//        p = new nod;
//        p->val = val;
//        p->st = NULL;
//        p->dr = NULL;
//    }
//    else
//    {
        if(p->val > val)
        {
            if(p->st)
            {
                insereaza(p->st, val);
            }
            else
            {
                p->st = new nod;
                p->st->val = val;
                p->st->st = NULL;
                p->st->dr = NULL;
            }
        }
        else
            if(p->val < val)
            {
                if(p->dr)
                {
                    insereaza(p->dr, val);
                }
                else
                {
                    p->dr = new nod;
                    p->dr->val = val;
                    p->dr->st = NULL;
                    p->dr->dr = NULL;
                }
    }
}

void citire()
{
    int val;
    fin>>n>>val;
    varf->val = val;
    varf->st = NULL;
    varf->dr = NULL;
    for(long i = 1; i < n; i++)
    {
        fin>>val;
        insereaza(varf, val);
    }
//    l[n-1] = 1;
}

void rezolvare(nod *p, int s)
{
    if(p)
    {
        rezolvare(p->dr, s+1);
//        std::cout<<p->val<<' '<<s+1<<'\n';;
        rezolvare(p->st, 1);
    }
    else
    {
        if(s > maxL)
        {
            maxL = s;
        }
//        std::cout<<s<<'\n';
    }
}

//void subsir(long vec[], long lung[], long nr)
//{
//    for(long i = nr - 2; i >= 0; i--)
//    {
//        long maxim = 0;
//        for(long j = i + 1; j < nr; j++)
//        {
//            if(vec[i] < vec[j] && lung[j] > maxim)
//            {
//                maxim = lung[j];
//            }
//        }
//        lung[i] = maxim+1;
//    }
//    long maxim = -1, ind = -1;
//    for(long i = 0; i < nr; i++)
//    {
//        if(lung[i] > maxim)
//        {
//            maxim = lung[i];
//            ind = i;
//        }
//    }
////    std::cout<<maxim<<'\n';
//    fout<<maxim<<'\n'<<vec[ind]<<' ';
//    long t = vec[ind];
//    for(long i = ind + 1; i < nr; i++)
//    {
//        if(vec[i] > t && lung[i] == maxim-1)
//        {
//            maxim--;
//            t = vec[i];
//            fout<<vec[i]<<' ';
//        }
//    }
////    fout<<'\n'<<'\n';
////    for(int i = 0; i < nr; i++)
////    {
////        fout<<lung[i]<<' ';
////    }
//}

int main()
{
    citire();
    rezolvare(varf, 1);
    fout<<maxL<<'\n';
//    subsir(a, l, n);
    return 0;
}