Cod sursa(job #1835095)

Utilizator TibixbAndrei Tiberiu Tibixb Data 26 decembrie 2016 12:38:02
Problema Subsir crescator maximal Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 1.98 kb
#include<fstream>
#include<algorithm>
#define NMAX 100005
using namespace std;
int n, k, x, poz, sol, d[NMAX], t[NMAX], poz_sol, i;
pair<int, int> A[4 * NMAX], b[NMAX], q;
ifstream cin("scmax.in");
ofstream cout("scmax.out");
void query(int st, int dr, int nod, int a, int c)
{
    if(a <= st && dr <= c)
    {
        if(A[nod].first > q.first && A[nod].first < b[i].first)
        {
            q = A[nod];
        }
        return;
    }
    int mij = st + (dr - st) / 2;
    if(a <= mij)
    {
        query(st, mij, 2 * nod, a, c);
    }
    if(c > mij)
    {
        query(mij + 1, dr, 2 * nod + 1, a, c);
    }
}

void update(int st, int dr, int nod, int poz, int x)
{
    if(st == dr)
    {
        A[nod].first = x;
        A[nod].second = poz;
        return;
    }
    int mij = st + (dr - st) / 2;
    if(poz <= mij)
    {
        update(st, mij, 2 * nod, poz, x);
    }
    else
    {
        update(mij + 1, dr, 2 * nod + 1, poz, x);
    }
    if(A[2 * nod].first > A[2 * nod + 1].first)
    {
        A[nod] = A[2 * nod];
    }else
    {
        A[nod] = A[2 * nod + 1];
    }
}

bool cmp(pair<int, int> a, pair<int, int> b)
{
    return a.second < b.second;
}

void drum(int poz)
{
    if(poz != 0)
    {
        drum(t[poz]);
        cout << b[poz].first << " ";
    }
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> b[i].first;
        b[i].second = i;
    }
    sort(b + 1, b + n + 1);

    for(i = 1; i <= n; i++)
    {
        q = make_pair(0, 0);

        poz = b[i].second;
        query(1, n, 1, 1, poz);
        d[poz] = q.first + 1;
        t[poz] = q.second;

        update(1, n, 1, poz, d[poz]);

        if(d[poz] > sol)
        {
            sol = d[poz];
            poz_sol = poz;
        }
    }

    sort(b + 1, b + n + 1, cmp);

    cout << sol << "\n";
    drum(poz_sol);
    return 0;
}