Cod sursa(job #2295874)

Utilizator ALEx6430Alecs Andru ALEx6430 Data 3 decembrie 2018 23:51:53
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>
#define MAX 100001
using namespace std;

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

int v[MAX],st,dr,k,t[MAX],p[MAX],n,pt,poz;

int caut_bin(int x)
{
    int st = 0, dr = k, m;

    while(st <= dr)
    {
        m = (st+dr)/2;
        if(v[t[m]] < x && v[t[m+1]] >= x) return m;
        else if(v[t[m+1]] < x) st = m+1;
        else dr = m-1;
    }

    return k;
}

void afis(int x)
{
    if(p[x]) afis(p[x]);

    out << v[x] <<' ';
}

int main()
{
    ios_base::sync_with_stdio(false);
    in.tie(0);

    in >> n;

    for(int i = 1; i <= n; i++) in >> v[i];
    t[1] = 1;

    k = 1;
    pt = 1;

    for(int i = 2; i <= n; i++)
    {
        poz = caut_bin(v[i]);

        p[i] = t[poz];
        t[poz+1] = i;

        if(poz+1 > k)
        {
            k = poz+1;
            pt = i;
        }
    }

    out << k << '\n';
    afis(pt);

    return 0;
}