Cod sursa(job #2144899)

Utilizator tanasaradutanasaradu tanasaradu Data 26 februarie 2018 23:31:59
Problema Subsir crescator maximal Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
const int Nmax = 100005;
int lis[Nmax] , top , poz[Nmax] , n , a[Nmax];
int main()
{
    int stg , drp , mij  , p , x , mx;
    fin >> n;
    fin >> x;
    a[1] = x;
    top = 1;
    lis[top] = x;
    poz[top] = 1;
    for(int i = 2 ; i <= n ; i++)
    {
        fin >> x;
        if(x <= lis[1])
        {
            lis[1] = x;
            poz[i] = 1;
        }
        else if(x > lis[top])
        {
            ++top;
            lis[top] = x;
            poz[i] = top;
        }
        else
        {
            stg = 1 , drp = top;
            while(stg <= drp)
            {
                mij = (stg + drp) / 2;
                if(lis[mij] >= x)
                {
                    p = mij;
                    drp = mij - 1;
                }
                else stg = mij + 1;
            }
            lis[p] = x;
            poz[i] = p;
        }
        a[i] = x;
    }
    fout << top << "\n";
    mx = top;
    top = 0;
    p = n + 1;
    a[p] = LONG_MAX;
    for(int i = n ; i >= 1 && mx > 0 ; i--)
        if(poz[i] == mx && a[i] < a[p])
    {
        --mx;
        p = i;
        lis[++top] = a[i];
    }
    for(int i = top ; i >= 1 ; i--)
        fout << lis[i] << " ";
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}