Cod sursa(job #2447815)

Utilizator pishcotMiruna Turbatu pishcot Data 14 august 2019 18:20:32
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, a[100005];
int st[100005], top;
int poz[100005];
int aux[100005];

int cautbin(int x)
{
    int mij, l = 1, dr = top, poz = 1;
    while(l <= dr)
    {
        mij = (l + dr) / 2;
        if(st[mij] >= x)
        {
            poz = mij;
            dr = mij - 1;

        }
        else l = mij + 1;
    }
    return poz;
}

int main()
{

    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> a[i];

    for(int i = 1; i <= n; i++)
    {
        if(a[i] > st[top])
            {
                st[++top] = a[i];
                poz[i] = top;
            }
        else if(a[i]<=st[1])
        {
            st[1]=a[i];
            poz[i]=1;
        }
        else
        {
            st[cautbin(a[i])] = a[i];
            poz[i] = cautbin(a[i]);
        }
    }
    fout << top << "\n";

    int x = top;

    for(int i = n; i >= 1 && x > 0; i--)
    {
        if(poz[i] == x)
        {
            aux[x] = a[i];
            x--;
        }
    }

    for(int  i = 1; i <= top; i++)
        fout << aux[i] << " ";

    return 0;
}