Cod sursa(job #2712071)

Utilizator Fatu_SamuelFatu Samuel Fatu_Samuel Data 25 februarie 2021 09:42:22
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int nMax = 100005;

int n;

int v[nMax], l[nMax], p[nMax];


int BinarySearch(int value, int length, int l[])
{
    int left = 1, right = length, mid;

    while (left <= right)
    {
        mid = (left + right) / 2;

        if (v[l[mid]] > value)
        {
            right = mid - 1;
        }
        else if (v[l[mid]] < value)
        {
            left = mid + 1;
        }
        else
        {
            return left;
        }
    }

    return left;
}

void WriteNumbers(int pos)
{
    if (pos == 0)
    {
        return;
    }

    WriteNumbers(p[pos]);
    fout << v[pos] << ' ';
}


int main()
{
    int k = 0;

    fin >> n;

    for (int i = 1; i <= n; i++)
    {
        fin >> v[i];

        int pos = BinarySearch(v[i], k, l);

        if (pos > k)
        {
            l[++k] = i;
        }
        else
        {
            l[pos] = i;
        }

        p[i] = l[pos - 1];
    }

    fout << k << '\n';

    WriteNumbers(l[k]);
    



    fin.close();
    fout.close();
    return 0;
}