Cod sursa(job #1841796)

Utilizator bt.panteaPantea Beniamin bt.pantea Data 6 ianuarie 2017 00:33:06
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.14 kb
#include <iostream>
#include <fstream>
#define DMAX 100010
//#define LimMax 2147483648 // 1<<31
#define LimMax 2000000000

using namespace std;

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

int n, maxBest;

struct AB
{
    int best;
    AB * right, * left;
};
inline int max (int a, int b)
{
    if (a > b) return a;
    else return b;
}
inline int GetValueBest (AB * p)
{
    if (p != NULL)
        return p->best;
    return 0;
}
inline AB * GetLeft(AB * p)
{
    if (p != NULL)
        return p->left;
    else return NULL;
}
inline AB * GetRight(AB * p)
{
    if (p != NULL)
        return p->right;
    else return NULL;
}
int GetBest(int x, int ls, int ld, AB * p)
{
    //cout<< ls <<' ' <<ld <<'\n';
    int m = (ls + ld + 1) / 2;

    if (ls == ld)
    {
        return GetValueBest(p);
    }
    if (x >= m && x <= ld)
        return max(GetValueBest(GetLeft(p)), GetBest(x, m, ld, GetRight(p)));
    else if (x < m)
        return GetBest(x, ls, m - 1, GetLeft(p));
    return 0;
}
void Update(int x, int value, int ls, int ld, AB * p)
{
    int m = (ls + ld + 1) / 2;
    if (ls == ld)
    {
        p->best = value;
        //cout<< x << ' ' << value <<'\n';
        return;
    }
    if (x < m)
    {
        if (p->left == NULL)
        {
            AB * q = new AB;
            q->left = NULL;
            q->right = NULL;
            q->best = 0;
            p->left = q;
        }
        Update(x, value, ls, m -1, p->left);
    }
    else
    {
        if (p->right == NULL)
        {
            AB * q = new AB;
            q->left = NULL;
            q->right = NULL;
            q->best = 0;
            p->right = q;
        }
        Update(x, value, m, ld, p->right);
    }
    p->best = max(GetValueBest(GetLeft(p)), GetValueBest(GetRight(p)));
}

int main()
{
    f >> n;
    AB * prim;
    prim->left = prim->right = NULL;
    for (int x, i = 0; i < n; i++)
    {
        f >> x;
        int best = GetBest(x, 1, LimMax, prim) + 1;
        Update(x, best, 1, LimMax, prim);
        maxBest = max(maxBest, best);
    }
    g<<maxBest;
    return 0;
}