Cod sursa(job #2102902)

Utilizator tanasaradutanasaradu tanasaradu Data 9 ianuarie 2018 16:38:33
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>
using namespace std;
const int Nmax =  100005;
int a[Nmax] , lis[Nmax] , top , aux[Nmax] , n , sol[Nmax] , k;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
inline int Binary_Search(int x)
{
    int st = 1 , dr = top , mij , poz = 0;
    while(st <= dr)
    {
        mij = ( st + dr ) / 2;
        if(lis[mij] >= x)
        {
            poz = mij;
            dr = mij - 1;
        }
        else st = mij + 1;
    }
   return poz;
}
int main()
{
    int p , mx;
    fin >> n;
    for(int i = 1 ; i <= n ; i++)
        fin >> a[i];
    lis[1] = a[1];
    top = 1;
    aux[1] = 1;
    for(int i = 2 ; i <= n ; i++)
    {
        int x;
        x = a[i];
        if(x <= lis[1])
        {
            lis[1] = x;
            aux[i] = 1;
        }
        else if(x > lis[top])
        {
            lis[++top] = x;
            aux[i] = top;
        }
        else
        {
            p = Binary_Search(x);
            lis[p] = x;
            aux[i] = p;
        }
    }
    fout << top << "\n";
    mx = top;
    p = LONG_MAX;
    for(int i = n ; i >= 1 ; i--)
        if(aux[i] == mx && a[i] < p)
        {
            p = a[i];
            ++k;
            sol[k] = a[i];
            mx--;
        }

    for(int i = k ; i >= 1 ; i--)
        fout << sol[i] << " ";
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}