Cod sursa(job #3287421)

Utilizator tudorhTudor Horobeanu tudorh Data 17 martie 2025 22:16:48
Problema Subsir crescator maximal Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
struct elem
{
    int val,pos;
}v[100001];

struct nod
{
    int best,pos;
}aint[400001];
bool sortval(elem a,elem b)
{
    return(a.val<b.val || (a.val==b.val && a.pos<b.pos));
}
nod combine(nod a,nod b)
{
    if(a.best>b.best)
        return a;
    if(b.best>a.best)
        return b;
    if(a.pos<=b.pos)
        return a;
    return b;
}
void update(int v,int st,int dr,int pos,nod a)
{
    if(st==dr)
        aint[v]=a;
    else
    {
        int mid=(st+dr)/2;
        if(pos<=mid)
            update(v*2,st,mid,pos,a);
        else update(v*2+1,mid+1,dr,pos,a);
        aint[v]=combine(aint[v*2],aint[v*2+1]);
    }
}
nod best;
void query(int v,int st,int dr,int qst,int qdr)
{

    if(qst<=st && qdr>=dr)
        best=combine(best,aint[v]);
    else
    {
        int mid=(st+dr)/2;
        if(qst<=mid)
            query(v*2,st,mid,qst,qdr);
        if(qdr>mid)
            query(v*2+1,mid+1,dr,qst,qdr);
    }
}
int main()
{
    int n,lmax=0;
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        fin>>v[i].val;
        v[i].pos=i;
    }
    sort(v+1,v+n+1,sortval);
    for(int i=1;i<=n;i++)
    {
        best={0,1000000};
        if(v[i].pos!=1)
            query(1,1,n,1,v[i].pos-1);
        if(best.best && v[i].val==v[best.pos].val)
            best.best--;
        update(1,1,n,v[i].pos,{best.best+1,i});
        lmax=max(lmax,best.best+1);
    }
    fout<<lmax;
    return 0;
}