Cod sursa(job #2576205)

Utilizator ana_maria_zotaZota Ana Maria ana_maria_zota Data 6 martie 2020 17:50:57
Problema Subsir crescator maximal Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n,a[100005],dp[100005],poz,lungime=1,p[100005];
int cb(int x)
{
    int st=1,dr=lungime;
    while (st<=dr)
    {
        int mij=(st+dr)/2;
        if (dp[mij]>=x)
        {
            dr=mij-1;
        }
        else
        {
            st=mij+1;
        }
    }
    if (st==lungime+1)
        return -1;
    return st;
}
 
 
int main( ) {
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        fin>>a[i];
        int poz=cb(a[i]);
        if(poz==-1)
        {
            dp[lungime]=a[i];
            lungime++;
            p[i]=lungime;
        }
        else
            
        {
            dp[poz]=a[i];
            p[i]=poz;
        }
    }
    lungime--;
    fout<<lungime<<"\n";
    for(int i=1;i<lungime;i++)
        fout<<dp[i]<<" ";
    
    return 0;
}