Cod sursa(job #2828351)

Utilizator NutaAlexandruASN49K NutaAlexandru Data 7 ianuarie 2022 09:31:12
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.51 kb
#include <stdio.h>
#include <cctype>
using namespace std;
class innput
{
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch()
    {
        ++sp;
        if(sp==4096)
        {
            sp=0;
            fread(buff,1,4096,fin);
        }
        return buff[sp];
    }

public:
    innput(const char* nume)
    {
        fin=fopen(nume,"r");
        buff=new char[4096]();
        sp=4095;
    }

    innput& operator >> (int &n)
    {
        char c;
        while(!isdigit(c=read_ch())&&c!='-');
        int sgn=1;
        if (c=='-')
        {
            n=0;
            sgn=-1;
        }
        else
        {
            n=c-'0';
        }
        while(isdigit(c=read_ch()))
        {
            n=10*n+c-'0';
        }
        n*=sgn;
        return *this;
    }

    innput& operator >> (long long &n)
    {
        char c;
        n=0;
        while(!isdigit(c=read_ch())&&c!='-');
        long long sgn=1;
        if(c=='-')
        {
            n=0;
            sgn=-1;
        }
        else
        {
            n=c-'0';
        }
        while(isdigit(c=read_ch()))
        {
            n=10*n+c-'0';
        }
        n*=sgn;
        return *this;
    }
};

class output
{
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch)
    {
        if(sp==20000)
        {
            fwrite(buff,1,20000,fout);
            sp=0;
            buff[sp++]=ch;
        }
        else
        {
            buff[sp++]=ch;
        }
    }

public:
    output(const char* name)
    {
        fout=fopen(name,"w");
        buff=new char[20000]();
        sp=0;
    }
    ~output()
    {
        fwrite(buff,1,sp,fout);
        fclose(fout);
    }

    output& operator <<(int vu32)
    {
        if(vu32<=9)
        {
            write_ch(vu32+'0');
        }
        else
        {
            (*this) <<(vu32/10);
            write_ch(vu32%10+'0');
        }
        return *this;
    }

    output& operator <<(long long vu64)
    {
        if(vu64<=9)
        {
            write_ch(vu64+'0');
        }
        else
        {
            (*this) <<(vu64/10);
            write_ch(vu64%10+'0');
        }
        return *this;
    }
    output& operator <<(char ch)
    {
        write_ch(ch);
        return *this;
    }
    output& operator <<(const char *ch)
    {
        while(*ch)
        {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};
//////////////////////////////////////////////////////
innput cin("peri.in");
output cout("peri.out");
const int N=1e5;
int a[N],lung[N],valmin[N],c;

int caut(int x)
{
    int l=1,r=c,rez=0;
    while(l<=r)
    {
        int m=(l+r)/2;
        if(valmin[m]<x)
        {
            rez=m;
            l=m+1;
        }
        else
        {
            r=m-1;
        }
    }
    return rez;
}
void rasp(int i)
{
    int p=i-1;
    while (p>=0&&(a[p]>=a[i] || lung[p]!=lung[i]-1))
    {
        p--;
    }
    if (p>=0)
    {
        rasp(p);
    }
    cout<<a[i]<<" ";
}
int main()
{
    int n;
    cin>>n;
    int maxx=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        int m=caut(a[i]);
        lung[i]=m+1;
        if(m==c)
        {
            c++;
        }
        valmin[m+1]=a[i];
        if(lung[i]>lung[maxx])
        {
            maxx=i;
        }
    }
    cout<<lung[maxx]<<"\n";
    rasp(maxx);
    return 0;
}