Cod sursa(job #2037401)

Utilizator gaina_gabriela@yahoo.comGaina Gabriela [email protected] Data 12 octombrie 2017 09:48:40
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
using namespace std;

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

const int MaxN = 100001, Inf = 0x3f3f3f3f;

int a[MaxN];
int v[MaxN];
int p[MaxN];
int val[MaxN];
int n;
int Lmax;

void Read();
void Sclm();
int BinarySearch(int valoare);
void WriteSolution();

int main()
{
   v[0] = - Inf;
   Read();
   Sclm();
   WriteSolution();
   return 0;
}
void Read()
{
    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> a[i];
}
int BinarySearch(int valoare)
{
    if (valoare > v[Lmax])
        return Lmax + 1;
    int l = 1, r = Lmax, m, poz = 1;
    while (l <= r)
    {
        m = (l + r) / 2;
        if (valoare <= v[m])
        {
            poz = m;
            r = m - 1;
        }
        else
            l = m + 1;
    }
    return poz;
}
void Sclm()
{
    int poz;
    for (int i = 1; i <= n; ++i)
    {
        poz = BinarySearch(a[i]);
        v[poz] = a[i];
        p[i] = poz;
        if (poz > Lmax)
            Lmax = poz;
    }

}

void WriteSolution()
{
    fout << Lmax << '\n';
    int k = Lmax, j = 0;
    for (int i = n; i >= 1; --i)
        if (p[i] == k)
        {
            val[++j] = a[i];
            k--;
        }
    for (int i = Lmax; i >= 1; --i)
        fout << val[i] << ' ';

}