Cod sursa(job #2549534)

Utilizator bem.andreiIceman bem.andrei Data 17 februarie 2020 19:23:34
Problema Subsir crescator maximal Scor 85
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include<fstream>

using namespace std;
ifstream r("scmax.in");
ofstream w("scmax.out");
int v[200001],poz[200001],dinamica[200001],cnt[200001], n, b, lungime, maxim, maxim2;
int cautbin(int x, int dr)
{
    int st=1, p=0, mij;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(v[poz[mij]]<x)
        {
            p=mij;
            st=mij+1;
        }
        else
            dr=mij-1;
    }
    return p;
}
int main()
{
    r>>n;
    for(int i=0; i<n; i++)
    {
        r>>v[i];
    }
    for(int i=1; i<=n; i++)
    {
        b=cautbin(v[i],lungime);
        lungime=max(lungime,b+1);
        dinamica[i]=b+1;
        poz[dinamica[i]]=i;
        maxim=max(maxim,dinamica[i]);
    }
    maxim2=maxim;
    w<<maxim<<"\n";
    for(int i=n;i>=1;i--)
    {
        if(maxim2==dinamica[i])
        {
            cnt[maxim2]=v[i];
            maxim2--;
        }
    }
    for(int i=1;i<=maxim;i++)
    {
        w<<cnt[i]<<" ";
    }
    return 0;
}