Cod sursa(job #2759171)

Utilizator rapidu36Victor Manz rapidu36 Data 15 iunie 2021 19:36:34
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>

using namespace std;

const int N = 1e5;

int a[N], l[N], sol[N], valmin[N+1];

int caut_bin(int x, int nr)
{
    ///cauta cel mai mare j cu propr ca valmin[j] < x
    int st = 1, dr = nr, j = 0;
    while (st <= dr)
    {
        int m = (st + dr) / 2;
        if (valmin[m] < x)///daca m e in zona "verde"
        {
            j = m;///m e un candidat pentru rez. final
            st = m + 1;
        }
        else
        {
            dr = m - 1;
        }
    }
    return j;
}

int main()
{
    ifstream in("scmax.in");
    ofstream out("scmax.out");
    int n, pmax = 0, lmax = 0;
    in >> n;
    for (int i = 0; i < n; i++)
    {
        in >> a[i];
        int j = caut_bin(a[i], lmax);
        if (j == lmax)
        {
            lmax++;
        }
        valmin[j+1] = a[i];
        l[i] = j + 1;
        if (l[i] > l[pmax])
        {
            pmax = i;
        }
    }
    out << l[pmax] << "\n";
    int vf = 0;
    sol[vf++] = a[pmax];
    for (int i = pmax; i >= 0; i--)
    {
        if (a[i] < a[pmax] && l[i] == l[pmax] - 1)
        {
            pmax = i;
            sol[vf++] = a[pmax];
        }
    }
    for (int i = vf - 1; i >= 0; i--)
    {
        out << sol[i] << " ";
    }
    in.close();
    out.close();
    return 0;
}