Cod sursa(job #2476445)

Utilizator CiboAndreiAndrei Cibo CiboAndrei Data 18 octombrie 2019 21:15:51
Problema Subsir crescator maximal Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>

using namespace std;

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

const int dim = 100002;

int n, nr;
int v[dim], best[dim], last[dim], pozEl[dim];

void afis(int i){
    if(pozEl[i] > 0)
        afis(pozEl[i]);

   g << v[i] << " ";
}

int caubin(int x){
    int p, u, m;
    p = 0; u = nr; m = (p+u)/2;

    while(p <= u){
        if (v[last[m]] < x && v[last[m + 1]] >= x)
            return m;
        else if(v[last[m+1]] < x){
            p = m + 1;
            m = (p + u)/2;
        } else {
            u = m - 1;
            m = (p + u)/2;
        }
   }

   return nr;
}

int main()
{
    int i, poz, mx = 0;

    f >> n;

    for(i = 1; i <= n; ++i)
        f >> v[i];

    best[1] = last[1] = 1;

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

        pozEl[i] = last[poz];
        best[i] = poz + 1;
        last[poz + 1] = i;

        nr = max(nr, poz + 1);
    }

    for(i = 1; i <= n; ++i)
        if(best[i] > mx)
            mx = best[i], poz = i;

    g << mx << '\n';

    afis(poz);

    return 0;
}