Cod sursa(job #2416124)

Utilizator GoogalAbabei Daniel Googal Data 26 aprilie 2019 22:13:49
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>

#define nmax 200001

using namespace std;

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

int n;
int a[nmax], b[nmax];
int poz[nmax];

int i, m, pas;

inline void read()
{
    in>>n;
    for(i=1; i<=n; i++)
        in>>a[i];
    in.close();
}

int cauta(int x)
{
    pas=1<<18;
    int r=0;

    while(pas!=0)
    {
        if(r+pas<=m && a[b[r+pas]]<x)
            r+=pas;

        pas/=2;
    }
    return r;
}

void write(int x)
{
    if(x>0)
    {
        write(poz[x]);

        out<<a[x]<<' ';
    }

}

inline void solve()
{
    int k;

    b[++m] = 1;

    for(i=2; i<=n; i++)
    {
        poz[i]=0;
        k=cauta(a[i]);
        k++;

        poz[i]=b[k-1];

        b[k]=i;

        if(k>m)
        {
            m=k;
        }
    }
}

int main()
{
    read();
    solve();

    out << m << '\n';

    write(b[m]);

    in.close();
    out.close();

    return 0;
}