Cod sursa(job #2855713)

Utilizator RTG123Razvan Diaconescu RTG123 Data 22 februarie 2022 20:22:38
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define MAXN 100001
#define zeroes(e) ((e^(e-1)) & e)
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int n,v[MAXN],aib[4*MAXN],besti[MAXN],r[MAXN],l[MAXN],maxt,rvalue,h=1,from[MAXN],maxl,afis[MAXN],cur;
void update (int x,int ind)
{
    for (; x<=n; x+=zeroes(x))
    {
        if (besti[ind]>besti[aib[x]])
        {
            aib[x]=ind;
        }
    }
}
int query (int x)
{
    int max1=0;
    for (; x; x-=zeroes(x))
    {
        if (besti[aib[x]]>besti[max1])
            max1=aib[x];
    }
    return max1;
}
int main()
{
    f>>n;
    for (int i=1; i<=n; i++)
    {
        f>>v[i];
        r[i]=l[i]=v[i];
    }
    sort(l+1,l+1+n);
    for (int i=2; i<=n; i++)
    {
        if (l[i]!=l[h])
        {
           // ++h;
            l[++h]=l[i];
            //++h;
        }
        //g<<h<<'\n';
    }
    for (int i=1; i<=n; i++)
    {
        v[i]=lower_bound(l+1,l+h+1,v[i])-l;
    }
    for (int i=1; i<=n; i++)
    {
        from[i]=query(v[i]-1);
        //cout<<from[i]<<' ';
        besti[i]=besti[from[i]]+1;
      //  cout<<besti[i]<<' '<<from[i]<<'\n';
        update(v[i],i);
    }
   // cout<<'\n';
    for (int i=1; i<=n; i++)
    {
        if (besti[i]>besti[maxl])
        {
            maxl=i;
        }
       // cout<<besti[i]<<' ';
    }
   // cout<<'\n';
    //cout<<besti[maxl]<<'\n';
    cur=maxl;
    h=0;
    for (int i=maxl; i; i=from[i])
    {
        afis[h]=r[i];
        h++;
    }
    g<<besti[maxl]<<'\n';
    for (int i=h-1; i>=0; i--)
    {
        g<<afis[i]<<' ';
    }
    return 0;
}