Cod sursa(job #2576376)

Utilizator ana_maria_zotaZota Ana Maria ana_maria_zota Data 6 martie 2020 18:51:06
Problema Subsir crescator maximal Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <iostream>
#include <stack>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n,a[100005],dp[100005],lungime=1,p[100005];
stack<int>stiva;
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;
}
void afisare( )
{
    for(int i=n;i>0;i--)
    {
        
        if((p[i]==lungime && stiva.top()>a[i] )||  stiva.empty())
        {
            stiva.push(a[i]);
            lungime--;
        }
    }
    while (!stiva.empty()) {
        fout<<stiva.top()<<" ";
        stiva.pop();
        
    }
    
}
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";
     afisare();
    return 0;
}