Cod sursa(job #832523)

Utilizator ericptsStavarache Petru Eric ericpts Data 10 decembrie 2012 20:53:29
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <cstdio>
using namespace std;
char *p;
#define maxn 100005
int v[maxn];
int longest[maxn];
int B[maxn];
int prec[maxn];
int L;
int n;
void scan(int &a)
{
    a&=0;
    while(*p < '0' || *p > '9')
        ++p;
    while(*p >= '0' && *p <= '9')
        a = a * 10 + *(p++)-'0';
}
void citire()
{
    long long len;
    fseek(stdin,0,SEEK_END);
    len = ftell(stdin);
    p = new char[len];
    rewind(stdin);
    fread(p,1,len,stdin);
}
void solve()
{
    int logN=1,step,found,i;
    for(i=1;i<=n;++i)
    {
        while((logN << 1) <= L)
            logN <<=1;
        for(step=logN,found=0;step;step>>=1)
        {
            if(found+step <= L && v[B[found+step]] < v[i])
                found |= step;
        }
        longest[i] = found+1;
        prec[i] = B[found];
        if(found == L)
        {
            ++L;
            B[L] = i;
        }
        else if(v[i] < v[B[found+1]])
            B[found+1]=i;
    }
}
void print(int i)
{
    if(i)
    {
        print(prec[i]);
        printf("%d ",v[i]);
    }
}
int main()
{
    freopen("scmax.in","r",stdin);
    freopen("scmax.out","w",stdout);
    int i;
    citire();
    scan(n);
    for(i=1;i<=n;++i)
        scan(v[i]);
    solve();
    for(i=n;i;--i)
        if(longest[i] == L)
        {
            printf("%d\n",L);
            print(i);
            return 0;
        }
    return 0;
}