Cod sursa(job #1375455)

Utilizator darrenRares Buhai darren Data 5 martie 2015 13:18:33
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <cstring>
#include <fstream>
#include <algorithm>

using namespace std;

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

int N;
int A[100002], B[100002], pos[100002], wh[100002];
int D[100002];
int AIB[100002], T[100002];

inline bool compare(const int& i1, const int& i2)
{
    return A[i1] < A[i2];
}

void update(int x, int pos)
{
    for (; x <= 100000; x += x & -x)
        if (D[pos] > D[AIB[x]])
            AIB[x] = pos;
}
int query(int x)
{
    int now = 0;
    for (; x >= 1; x -= x & -x)
        if (now == 0 || D[AIB[x]] > D[now])
            now = AIB[x];
    return now;
}

void track(int x)
{
    if (x == 0) return;
    track(T[x]);

    fout << wh[B[x]] << ' ';
}

int main()
{
    fin >> N;
    for (int i = 1; i <= N; ++i)
    {
        fin >> A[i];
        pos[i] = i;
    }
    sort(pos + 1, pos + N + 1, compare);

    int tm = 0;
    for (int i = 1; i <= N; ++i)
    {
        ++tm;
        wh[tm] = A[pos[i]];

        int now = i;
        while (now <= N && A[pos[i]] == A[pos[now]])
        {
            B[pos[now]] = tm;
            ++now;
        }
        i = now - 1;
    }

    int whres = 0;
    for (int i = 1; i <= N; ++i)
    {
        int wh = query(B[i] - 1);

        D[i] = D[wh] + 1;
        T[i] = wh;

        if (whres == 0 || D[i] > D[whres])
            whres = i;

        update(B[i], i);
    }

    fout << D[whres] << '\n';
    track(whres);

    fin.close();
    fout.close();
}