Cod sursa(job #2795280)

Utilizator Robert.BrindeaBrindea Robert Robert.Brindea Data 6 noiembrie 2021 10:33:43
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

const int MAXN = 100000;
int n, k, a[MAXN+3], s[MAXN+3], pos[MAXN+3];

int cautBin(int x)
{
    int p = 1, i, l;
    while(p*2 <= n) p*=2;
    for(i = 0, l=p; l; l>>=1)
        if(i+l <= n && a[i+l] <= x) i+=l;
    return i;
}

vector<int>sol;

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> a[i];
    for(int i = 1; i <= n; i++)
    {
        if(k == 0 || s[k] < a[i])
        {
            s[++k] = a[i];
            pos[k] = i;
        }
        else
        {
            int posElem = cautBin(a[i]);
            s[posElem] = a[i];
            pos[i] = posElem;
        }
    }
    fout << k << "\n";
    for(int i = n; i; i--)
        if(pos[i] == k)
        {
            sol.push_back(a[i]);
            k--;
        }
    for(int i = sol.size()-1; i >= 0; i--)
        fout << sol[i] << " ";
    return 0;
}