Cod sursa(job #2566669)

Utilizator alex_braslasuBraslasu Alexandru alex_braslasu Data 2 martie 2020 23:06:25
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>
#include <climits>

using namespace std;
ifstream f("scmax.in");;
ofstream g("scmax.out");

int n, i, j, a[100010], b[100010], k = 0, s;

int Caut(int st, int dr, int x)
{
    int r = INT_MAX;
    while (st < dr)
    {
        int m = (st + dr) / 2;
        if (x >= b[m])
            st = m + 1;
        else
        {
            if (r != INT_MAX)
            {
                if (b[m] < b[r])
                    r = m;
            }
            else
                r = m;
            dr = m;
        }
    }
    return r;
}

int main()
{
    f >> n;
    for (i = 1; i <= n; ++i)
    {
        f >> a[i];
        if (k > 0)
        {
            if (k != 1)
            {
                if (a[i] > b[k])
                {
                    ++k;
                    b[k] = a[i];
                }
                else
                {
                    s = Caut(1, k, a[i]);
                    if (s != INT_MAX)
                        b[s] = a[i];
                }
            }
            else
            {
                if (a[i] > b[k])
                {
                    ++k;
                    b[k] = a[i];
                }
                else
                    b[k] = a[i];
            }
        }
        else
        {
            ++k;
            b[k] = a[i];
        }
    }
    g << k << '\n';
    for (i = 1; i <= k; ++i)
        g << b[i] << " ";
    return 0;
}