Cod sursa(job #1041453)

Utilizator tudorv96Tudor Varan tudorv96 Data 25 noiembrie 2013 20:31:39
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

const int N = 1e5 + 5;

int n, p[N], v[N], last[N], bst[N], hi = 1, AIB[N], tata[N], MAX, sol[N];

int bs(int x) {
    int lower = 1, higher = hi + 1;
    while (lower <= higher) {
        int mid = (lower + higher) >> 1;
        if (last[mid] == x)
            return mid;
        else
            if (last[mid] < x)
                lower = mid + 1;
        else
            higher = mid;
    }
    return 0;
}

void update (int x, int i) {
    while (x <= n) {
        if (bst[i] > bst[AIB[x]])
            AIB[x] = i;
        x += (x ^ (x - 1)) & x;
    }
}

int query (int x) {
    int MAX = 0;
    while (x) {
        if (bst[MAX] < bst[AIB[x]])
            MAX = AIB[x];
        x -= (x ^ (x - 1)) & x;
    }
    return MAX;
}

int main() {
    fin >> n;
    for (int i = 1; i <= n; ++i) {
        fin >> v[i];
        last[i] = v[i];
    }
    fin.close();
    sort (last+1, last + n + 1);
    for (int i = 2; i <= n; ++i)
        if (last[hi] != last[i])
            last[++hi] = last[i];
    for (int i = 1; i <= n; ++i)
        p[i] = bs(v[i]);
    for (int i = 1; i <= n; ++i) {
        tata[i] = query (p[i] - 1);
        bst[i] = bst[tata[i]] + 1;
        update (p[i], i);
        if (bst[i] > bst[MAX])
            MAX = i;
    }
    fout << bst[MAX] << "\n";
    int s = 0;
    while (MAX) {
        sol[++s] = v[MAX];
        MAX = tata[MAX];
    }
    for (int i = s; i; --i)
        fout << sol[i] << " ";
    fout.close();
}