Cod sursa(job #2691860)

Utilizator tomaionutIDorando tomaionut Data 30 decembrie 2020 12:48:49
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

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

    return 0;
}