Cod sursa(job #2512487)

Utilizator radumihaisirbuSirbu Radu-Mihai radumihaisirbu Data 21 decembrie 2019 10:49:49
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 1e5 + 2;

int a[nmax], n, ext, poz, d[nmax], t[nmax];

void read ()
{
    fin >> n;
    for (int i=1; i<=n; ++i) fin >> a[i];
}

int bin (int x)
{
    int ls = 1, ld = ext, mij;

    while(ls <= ld)
    {
        mij = (ls + ld) / 2;
        if (a[d[mij]] < x) ls = mij + 1;
        else ld = mij - 1;
    }

    return ls;
}

void write (int val)
{
    if (ext <= 0) return;

    for (int i=val; i>=1; --i)
        if (t[i] == ext)
        {
            ext--;
            write(i-1);
            fout << a[i] << ' ';
            return;
        }
}

int main()
{
    read();

    for (int i=1; i<=n ;++i)
    {
        poz = bin(a[i]);
        t[i] = poz;
        if (poz > ext)
        {
            ext++;
            d[ext] = i;
        }
        else if (a[i] < a[d[poz]]) d[poz] = i;
    }

    fout << ext << '\n';

    write(n);

    return 0;
}