Cod sursa(job #2250888)

Utilizator roberttrutaTruta Robert roberttruta Data 30 septembrie 2018 19:54:58
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>

using namespace std;
int n,L_max,L_sbsc,t,v[100005],vf_min[100005],poz[100005];
///vf_min[i]=poz celui mai mic ultim numar dintr-o subsecventa de i nr.
int cauta(int s, int d, int poz_nr)
{
    int m=0;
    while(s<=d)
    {
        m=(s+d)/2;
        if(v[vf_min[m]]<v[poz_nr] && v[vf_min[m+1]]>=v[poz_nr])
            return m+1;
        else
        {
            if(v[vf_min[m]]>=v[poz_nr])
            d=m-1;
            else
            s=m+1;
        }
    }
    return m;
}
int main()
{
    ifstream f("scmax.in");
    ofstream g("scmax.out");
int i;
    f>>n;
    for(i=1;i<=n;i++)
        f>>v[i];
    vf_min[1]=1;
    L_max=1;
    for(i=2;i<=n;i++)
    {
        if(v[i]>v[vf_min[L_max]])
        {
            poz[i]=vf_min[L_max];
            vf_min[++L_max]=i;
        }
       else
        {
            L_sbsc=cauta(1,L_max,i);
            poz[i]=vf_min[L_sbsc-1];
            vf_min[L_sbsc]=i;
        }
    }
    g<<L_max<<'\n';
    i=vf_min[L_max];
    while(i)
    {
        vf_min[++t]=v[i];
        i=poz[i];
    }
    for(i=t;i>=1;i--)
        g<<vf_min[i]<<' ';

    return 0;
}