Cod sursa(job #2492427)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 14 noiembrie 2019 18:57:40
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
#define NMAX 100005

using namespace std;

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

int n;
int a[NMAX] , pos[NMAX] , ans[NMAX] , aux[NMAX];

int Binary_Search(int val , int &k)
{
    if(aux[k] < val)
    {
        aux[++k] = val;
        return k;
    }

    int st = 1 , dr = k , mij = 0 , pos = 0;

    while(st <= dr)
    {
        mij = (dr - st) / 2 + st;

        if(aux[mij] >= val)
        {
            pos = mij;
            dr = mij - 1;
        }
        else st = mij + 1;
    }

    aux[pos] = val;
    return pos;
}

int main()
{
    int i , k = 0;

    f >> n;

    for(i = 1 ; i <= n ; i++)
    {
        f >> a[i];
        pos[i] = Binary_Search(a[i] , k);
    }

    int m = k;

    for(i = n ; i >= 1 ; i--)
    {
        if(pos[i] == m)
            ans[m--] = a[i];
    }

    g << k << '\n';

    for(i = 1 ; i <= k ; i++)
        g << ans[i] << ' ';

    return 0;
}