Cod sursa(job #860616)

Utilizator informatician28Andrei Dinu informatician28 Data 20 ianuarie 2013 14:38:36
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>

using namespace std;

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

#define DIM 100001

int N, V[DIM], Q[DIM], P[DIM], length, ind[DIM], contor;

int bs (int nr)
{
    int lo, hi, mid, pos = 0;
    lo = 1, hi = length;
    while (lo <= hi)
    {
        mid = (lo + hi)/2;
        if (nr <= Q[mid]) pos = mid, hi = mid - 1;
        else lo = mid + 1;
    }
    if (pos == 0) return ++ length;
    else return pos;
}

int main()
{
    int i;
    in >> N;
    for (i = 1; i <= N; i++)
    {
        in >> V[i];
    }
    length = 1;
    Q[1] = V[1], P[1] = length;
    for (i = 2; i <= N; i++)
    {
        int pos = bs(V[i]);
        Q[pos] = V[i], P[i] = pos;
    }
    out << length << '\n';
    for (i = N; i >= 1; i--)
    {
        if (P[i] == length) ind[++contor] = i, length --;
    }
    for (i = contor; i >= 1; i--)
    {
        out << V[ind[i]] << " ";
    }
    return 0;
}