Cod sursa(job #3222244)

Utilizator zavragiudavid dragoi zavragiu Data 9 aprilie 2024 15:53:31
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <algorithm>
#include <bitset>
#include <iostream>
#include <stack>

using namespace std;

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

int a[100001], dp[100001], poz[100001], k;

int CautBin (int val)
{
    int st = 1, dr = k, mij, p = 0;
    while (st <= dr)
    {
        mij = (st + dr) / 2;
        if (dp[mij] >= val)
        {
            p = mij;
            dr = mij - 1;
        }
        else st = mij + 1;
    }
    return p;
}

int main()
{
    int n;
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> a[i];
    dp[1] = a[1];
    poz[1] = 1;
    k = 1;
    for (int i = 2; i <= n; i++)
        if (a[i] > dp[k])
        {
            dp[++k] = a[i];
            poz[i] = k;
        }
        else
        {
            int p = CautBin(a[i]);
            dp[p] = a[i];
            poz[i] = p;
        }
    fout << k << "\n";
    int pozmax = 1;
    for (int i = 2; i <= n; i++)
        if (poz[pozmax] < poz[i]) pozmax = i;
    stack<int> st;
    st.push(pozmax);
    for (int i = pozmax - 1; i >= 1; i--)
        if (poz[i] == poz[pozmax] - 1)
        {
            st.push(i);
            pozmax = i;
        }
    while (!st.empty())
    {
        fout << a[st.top()] << " ";
        st.pop();
    }
    return 0;
}