Cod sursa(job #1152578)

Utilizator japjappedulapPotra Vlad japjappedulap Data 24 martie 2014 20:19:12
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
using namespace std;

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

int n;
int D[100002], V[100002], T[100002], B[100002], lg = 1;

int BS(int x);
void Out(int i);

int main()
{
    is >> n;
    for (int i = 1; i <= n; ++i)
        is >> V[i];
    D[1] = B[1] = 1; B[0] = 0;
    int pos;
    for (int i = 2; i <= n; ++i)
    {
        pos = BS(V[i]);
        T[i] = B[pos];
        D[i] = pos+1;
        B[pos+1] = i;
        if (lg < pos+1) lg = pos+1;
    }
    os << lg << '\n';
    pos = B[lg];
    Out(pos);
    is.close();
    os.close();
    return 0;
}

int BS(int x)
{
    int L = 0, R = lg, M;
    while (L <= R)
    {
        M = (L+R)/2;
        if (V[B[M]] < x && V[B[M+1]] >= x) return M;
        else if (V[B[M]] < x)   L = M+1;
        else                    R = M-1;
    }
    return lg;
};

void Out(int i)
{
    if (T[i] > 0) Out(T[i]);
    os << V[i] << ' ';
};