Cod sursa(job #2281138)

Utilizator mjmilan11Mujdar Milan mjmilan11 Data 11 noiembrie 2018 16:25:04
Problema Subsir crescator maximal Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 100005;
const int INF = (1<<29);

int a[NMAX],v[NMAX],ind[NMAX];
int pre[NMAX],aux[NMAX];

int cautarebinara(int val)
{
    int st=1;
    int dr=NMAX-4,mij;
    int rasp;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(val<=a[v[mij]])
        {
            dr=mij-1;
            rasp=mij;
        }
        else
        {
            st=mij+1;
        }
    }
    return rasp;
}

int main()
{
    int n;
    fin >> n;
    int poz;
    for(int i=1;i<=NMAX-1;i++) v[i]=n+1;
    for(int i=1;i<=n;i++) fin >> a[i];
    a[n+1]=INF;
    for(int i=1;i<=n;i++)
    {
        poz=cautarebinara(a[i]);
        v[poz]=i;
        pre[i]=v[poz-1];
    }
    int lungime=1;
    for(int i=1;i<=n;i++)
    {
        if(v[i]==n+1)
        {
            lungime=i-1;
            break;
        }
    }
    fout << lungime << '\n';
    int ind=v[lungime];
    int k=0;
    while(ind!=0)
	{
       aux[++k]=ind;
       ind=pre[ind];
    }
    ///for(int i=1;i<=k;i++) fout << aux[i] << ' ';
    for(int i=k;i>=1;i--) fout << a[aux[i]] << ' ';
    return 0;
}