Cod sursa(job #2169536)

Utilizator calin1Serban Calin calin1 Data 14 martie 2018 16:01:47
Problema Subsir crescator maximal Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <cstdio>
#include <stack>
#define N 100005

using namespace std;

int n, a[N], s1[N], s2[N], l_s1 = 1, l_s2 = 1;

stack <int> de_afisat;

int cautare_binara(int x)
{
    int st = 1;
    int dr = l_s1 - 1;

    int mij;

    while(st < dr)
    {
        mij = (st + dr) >> 1;

        if(x < s1[mij])
        {
            dr = mij;
        }
        else
        {
            st = mij + 1;
        }
    }

    return st;
}

void prelucrare()
{
    printf("%d\n", l_s1 - 1);

    for(int i = n ; i > 0 ; --i)
    {
        if(s2[i] == l_s1 - 1)
        {
            de_afisat.push(a[i]);

            l_s1--;
        }
    }

    int x;

    while(!de_afisat.empty())
    {
        x = de_afisat.top();

        de_afisat.pop();

        printf("%d ", x);
    }
}

void citire()
{
    scanf("%d\n", &n);

    int poz;

    for(int i = 1 ; i <= n ; ++i)
    {
        scanf("%d ", &a[i]);

        poz = cautare_binara(a[i]);

        if(s1[poz] >= a[i])
        {
            s1[poz] = a[i];

            s2[l_s2++] = poz;
        }
        else
        {
            s1[l_s1] = a[i];

            s2[l_s2++] = l_s1++;
        }
    }

    prelucrare();
}

int main()
{
    freopen("scmax.in" , "r" , stdin);
    freopen("scmax.out" , "w" , stdout);

    citire();

    return 0;
}