Cod sursa(job #736238)

Utilizator BitOneSAlexandru BitOne Data 18 aprilie 2012 10:59:54
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.96 kb
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011
#define bufferSize 1024

using namespace std;
 ifstream in("scmax.in");
ofstream out("scmax.out");

int N, bufferIndex=-1;
char buffer[bufferSize];
int v[N_MAX], w[N_MAX], index[N_MAX], l[N_MAX], f[N_MAX], aib[N_MAX];

inline void Read(int& x)
{
    if(-1 == bufferIndex)
    {
        bufferIndex=0;
        in.read(buffer, N_MAX);
    }
    while(buffer[bufferIndex] < '0' || buffer[bufferIndex] > '9')
        if(++bufferIndex == bufferSize)
        {
            bufferIndex=0;
            in.read(buffer, N_MAX);
        }
    for(x=0; buffer[bufferIndex] >= '0' && buffer[bufferIndex] <= '9'; )
    {
        x=x*10+buffer[bufferIndex]-'0';
        if(++bufferIndex == bufferSize)
        {
            bufferIndex=0;
            in.read(buffer, N_MAX);
        }
    }
}
inline void Update(int xIndex, int& xValue)
{
    for(; xIndex <= N; xIndex+=(xIndex & -xIndex) )
        if(l[aib[xIndex]] < l[xValue])
            aib[xIndex]=xValue;
}
inline int Query(int xIndex)
{
    int m=0;

    for(; xIndex; xIndex-=(xIndex & -xIndex) )
        if(l[aib[xIndex]] > l[m])
            m=aib[xIndex];

    return m;
}
int main()
{
    int *iend;
    int i, j, maxIndex=0;

    Read(N);
    for(i=1; i <= N; ++i)
    {
        Read(v[i]);
        w[i]=v[i];
    }
    sort(w+1, w+N+1);
    iend=unique(w+1, w+N+1);
    for(i=1; i <= N; ++i)
        if(v[i] == v[i-1])
            index[i]=index[i-1];
        else index[i]=lower_bound(w, iend, v[i])-w;
    for(i=1; i <= N; ++i)
    {
        f[i]=Query(index[i]-1);
        l[i]=l[f[i]]+1;
        Update(index[i], i);
        if(l[i] > l[maxIndex])
            maxIndex=i;
    }
    out<<l[maxIndex]<<'\n';
    for(i=maxIndex, j=l[maxIndex]; i; i=f[i], --j)
        w[j]=v[i];
    copy(w+1, w+l[maxIndex]+1, ostream_iterator<int>(out, " "));

    return EXIT_SUCCESS;
}