Cod sursa(job #2560977)

Utilizator alex_braslasuBraslasu Alexandru alex_braslasu Data 28 februarie 2020 13:52:42
Problema Subsir crescator maximal Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <algorithm>

#define zeros(x) (x^(x-1)&x)
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");

int n, i, h = 1, v[100010], AIB[100010], lst[100010], D[100010], up[100010], bst;

void update(int x, int ind)
{
    for (int i = x; i <= n; i += zeros(i))
        if (D[ind] > D[AIB[i]])
           AIB[i] = ind;
}

int query(int x)
{
    int mx = 0;
    for (int i = x; i >= 1; i -= zeros(i))
        if (D[AIB[i]] > D[mx])
           mx = AIB[i];
    return mx;
}

int main()
{
    f >> n;
    for (i = 1; i <= n; ++i)
    {
        f >> v[i];
        lst[i] = v[i];
    }
    sort(lst + 1, lst + n + 1);
    for (i = 2; i <= n; ++i)
        if (lst[i] != lst[h])
           lst[++h] = lst[i];
    for (i = 1; i <= n; ++i)
        v[i] = lower_bound(lst + 1, lst + h + 1, v[i]) - lst;
    for (i = 1; i <= n; ++i)
    {
        up[i] = query(v[i] - 1);
        D[i] = D[up[i]] + 1;
        update(v[i], i);
    }
    for (i = 1; i <= n; ++i)
        if (D[i] > D[bst])
           bst = i;
    g << D[bst];
    return 0;
}