Cod sursa(job #1695739)

Utilizator superstar1998Moldoveanu Vlad superstar1998 Data 27 aprilie 2016 18:51:52
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.84 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cctype>

#define pb push_back
#define mp make_pair
#define MAXN 500001
#define DIM 8192
#define INFILE "scmax.in"
#define OUTFILE "scmax.out"

using namespace std;

class Reader
{
public:
    Reader(const char* filename)
    {
        f.open(filename);
        pos = 0;
        f.read(buffer,DIM);
    }
    inline Reader& operator >>(int& x)
    {
        sgn=1;
        while((buffer[pos]<'0'||buffer[pos]>'9')&&buffer[pos]!='-')
            if(++pos==DIM)
                f.read(buffer,DIM),pos=0;
        if(buffer[pos]=='-')sgn=-1,pos++;
        while(buffer[pos]>='0'&&buffer[pos]<='9')
        {
            x=x*10+buffer[pos]-'0';
            if(++pos==DIM)f.read(buffer,DIM),pos=0;
        }
        x*=sgn;
        return (*this);
    }
    inline Reader& operator >>(char* c)
    {
        aux = 0;
        while(isspace(buffer[pos]))
            if(++pos==DIM)
                f.read(buffer,DIM),pos=0;
        while(!isspace(buffer[pos]))
        {
            c[aux++]=buffer[pos];
            if(++pos==DIM)f.read(buffer,DIM),pos=0;
        }
        return (*this);
    }
    ~Reader()
    {
        f.close();
    }
private:
    ifstream f;
    int pos,aux,sgn;
    char buffer[DIM];
};

class Writer
{
public:
    Writer(const char* filename)
    {
        g.open(filename);
        pos = 0;
    }
    inline Writer& operator <<(int xx)
    {
        aux = 0;
        x = xx;
        if(xx<0)x=-x;
        if(x==0)
            nr[aux++]='0';
        while(x)
        {
            nr[aux++]=x%10+'0';
            x/=10;
        }
        if(xx<0)nr[aux++]='-';
        while(aux>0)
        {
            buffer[pos++]=nr[--aux];
            if(pos==DIM)g.write(buffer,DIM),pos=0;
        }
        return (*this);
    }
    inline Writer& operator <<(const char* c)
    {
        aux = 0;
        while(c[aux])
        {
            buffer[pos++]=c[aux++];
            if(pos==DIM)g.write(buffer,DIM),pos=0;
        }
        return (*this);
    }
    ~Writer()
    {
        g.write(buffer,pos);
        g.close();
    }
private:
    ofstream g;
    int pos,aux,x;
    char buffer[DIM],nr[21];
};

Reader f(INFILE);
Writer g(OUTFILE);
int n,a[100001],l[100001],i,j;
int main()
{
    f>>n;
    for(i=1;i<=n;i++)
        f>>a[i];
    int Max;
    l[n]=1;
    for(i=n-1;i>0;i--)
    {
        Max=0;
        for(j=i+1;j<=n;j++)
            if(a[i]<a[j]&&Max<l[j])Max=l[j];
        l[i]=Max+1;
    }
    Max=0;
    int k;
    for(i=1;i<=n;i++)
        if(l[i]>Max)Max=l[i],k=i;
    g<<Max<<"\n";
    for(i=k;i<=n&&Max;i++)
        if(l[i]==Max)
        {
            g<<a[i]<<" ";
            Max--;
        }
    return 0;
}