Cod sursa(job #2865554)

Utilizator tomaionutIDorando tomaionut Data 8 martie 2022 22:11:48
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");
int a[100005], n, k, dp[100005], poz[100005], sol[100005];
int CautBin(int x)
{
    int st, dr, sol, mij;
    st = 1;
    dr = sol = k;
    while (st <= dr)
    {
        mij = (st + dr) / 2;
        if (dp[mij] >= x)
        {
            sol = mij;
            dr = mij - 1;
        }
        else st = mij + 1;
    }
    return sol;
}
int main()
{
    int i, x, p;
    fin >> n;
    for (i = 1; i <= n; i++)
        fin >> a[i];

    for (i = 1; i <= n; i++)
    {
        x = a[i];
        if (x <= dp[1]) { dp[1] = x; poz[i] = 1; }
        else if (x > dp[k]) { dp[++k] = x; poz[i] = k; }
        else 
        {
            p = CautBin(x);
            dp[p] = x;  
            poz[i] = p;
        }
    }

    fout << k << "\n";
    x = k;
    for (i = n; i >= 1 and x > 0; i--)
        if (poz[i] == x)
        {
            sol[x] = a[i];
            x--;
        }
    
    for (i = 1; i <= k; i++)
        fout << sol[i] << " ";


    return 0;
}