Cod sursa(job #3240472)

Utilizator mihaigeorgescuGeorgescu Mihai mihaigeorgescu Data 15 august 2024 18:50:02
Problema Secv Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <fstream>

using namespace std;
ifstream fcin("secv.in");
ofstream fout("secv.out");
int n,v[100001],u[100001],m,vf,nr,vf1,sc[100001],poz,next1[100001],first,last;
int lowerbound(int val, int dr)
{
    int st=1;
    int mij;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(val<=v[u[mij]])
        {
            dr=mij-1;
        }
        else
        {
            st=mij+1;
        }
    }
    return dr;
}
int main()
{
    fcin>>n;
    for(int i=1; i<=n; i++)
    {
        fcin>>v[i];
    }
    vf=1;
    u[1]=1;
    for(int i=2; i<=n; i++)
    {
        int j=lowerbound(v[i], vf);
        next1[i]=u[j];
        u[j+1]=i;
        if(j+1>vf)
        {
            vf=j+1;
            poz=u[j+1];
        }
    }
    last=poz;
    while(vf--)
    {
        first=poz;
        vf1++;
        sc[vf1]=v[poz];
        poz=next1[poz];
    }
    fout<<last-first+1;

    return 0;
}