Cod sursa(job #2554081)

Utilizator cyg_Alex_codegicianBarbu Alexandru cyg_Alex_codegician Data 22 februarie 2020 16:10:45
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int nmax=100005;
int a[nmax],tatic[nmax],n;
void afisare(int n)
{
    if (tatic[n]) afisare(tatic[n]);
    fout << a[n] << " ";
}
int main()
{
    int dp[nmax],poz,st,dr,mid;
    fin >> n;
    for (int i=1;i<=n;i++)
    {
        fin >> a[i];
    }
    poz=1;
    dp[1]=1;
    for (int i=2;i<=n;i++)
    {
        st=1;
        dr=poz;
        while (st<=dr)
        {
            mid=(st+dr)/2;
            if (a[dp[mid]]>=a[i])
                dr=mid-1;
            else
                st=mid+1;
        }
        if (st>poz)
        {
            poz++;
            dp[poz]=i;
            tatic[i]=dp[st-1];
        }
        else
        {
            dp[st]=i;
            tatic[i]=dp[st-1];
        }
    }
    fout << poz << '\n';
    afisare(dp[poz]);
}