Cod sursa(job #2256172)

Utilizator Victor_InczeVictor Incze Victor_Incze Data 8 octombrie 2018 09:43:24
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
#define MAXN 100005

using namespace std;

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

int n, v[MAXN], p[MAXN], L[MAXN], best[MAXN], maxv, poz, nr;

void afis(int i)
{
    if (p[i])
        afis(p[i]);
    out << v[i] << " ";
}

int caut(int x)
{
    int p, u, m;
    p=0;
    u=nr;
    m=nr/2;
    while (p<=u)
    {
        if (v[L[m]]<x && v[L[m+1]]>=x)
            return m;
        if (v[L[m+1]]<x)
        {
            p=m+1;
            m=(p+u)/2;
        }
        else
        {
            u=m-1;
            m=(p+u)/2;
        }
    }
    return nr;
}

int main()
{
    in >> n;
    for (int i=1; i<=n; i++)
        in >> v[i];
    best[1]=L[1]=1;
    nr=1;
    for (int i=2; i<=n; i++)
    {
        poz=caut(v[i]);
        p[i]=L[poz];
        best[i]=poz+1;
        L[poz+1]=i;
        if (nr<poz+1)
            nr=poz+1;
    }
    for (int i=1; i<=n; i++)
        if (maxv<best[i])
        {
            maxv=best[i];
            poz=i;
        }
    out << maxv << '\n';
    afis(poz);
    return 0;
}