Cod sursa(job #1790585)

Utilizator antanaAntonia Boca antana Data 28 octombrie 2016 14:41:15
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.36 kb
#include <cstdio>
#include <algorithm>
#include <cctype>
#define MAXN 100000
#define BUF_SIZE (1<<17)

struct aa{
    int nr, id;
}aux[MAXN+1];

char buf[BUF_SIZE], buf2[BUF_SIZE];
int pos1 = BUF_SIZE, pos2, n, d[MAXN+1], v[MAXN+1], aib[MAXN+1], last[MAXN+1], name[MAXN+1];

inline int getInt();
inline char getChar();
inline void putch(char);
inline void scrie1(int);

bool cmp(const aa &v1, const aa &v2)
{
    if(v1.nr == v2.nr)
        return (v1.id > v2.id);
    return (v1.nr < v2.nr);
}

inline void update(int x, int pos)
{
    int i = x;
    while(i<=n)
    {
        if(d[pos] > d[aib[i]])
            aib[i] = pos;
        i += (i&-i);
    }
}

inline int query(int ind)
{
    int pos = 0;
    while(ind > 0)
    {
        if(d[pos] < d[aib[ind]])
            pos = aib[ind];
        ind -= (ind & -ind);
    }
    return pos;
}

void scrie(int pos)
{
    if(last[pos] != 0)
        scrie(last[pos]);

    scrie1(name[v[pos]]);
    putch(' ');
}

FILE *fin, *fout;


int main()
{
    fin  = fopen("scmax.in", "r");
    fout = fopen("scmax.out", "w");

    int r, maxs=-1, pos, i;

    n = getInt();

    for(i=1; i<=n; ++i)
        aux[i].nr = getInt(), aux[i].id = i;

    std::sort(aux+1, aux+n+1, cmp);

    for(i=1; i<=n; ++i){
        v[aux[i].id] = i;
        name[i] = aux[i].nr;
    }

    for(i=1; i<=n; ++i){
        r = query(v[i]-1);
        d[i] = d[r] + 1;
        last[i] = r;
        update(v[i], i);
    }

    for(i=1; i<=n; ++i)
        if(d[i] > maxs)
            maxs = d[i], pos = i;

    scrie1(maxs);
    putch('\n');
    scrie(pos);

    if(pos2) fwrite(buf2, 1, pos2, fout);

    return 0;
}

inline char getChar()
{
    if(pos1 == BUF_SIZE) pos1 = 0, fread(buf, 1, BUF_SIZE, fin);
    return buf[pos1++];
}

inline int getInt()
{
    int nr = 0;
    char c;

    c = getChar();
    while(!isdigit(c)) c = getChar();
    while(isdigit(c))
    {
        nr = nr*10 + c-'0';
        c = getChar();
    }

    return nr;
}

inline void putch(char c)
{
    buf2[pos2++] = c;
    if(pos2 == BUF_SIZE) pos2 = 0, fwrite(buf2, 1, BUF_SIZE, fout);
}

inline void scrie1(int nr)
{
    char s[10];
    int k = 0;

    do{
        s[k++] = nr%10 + '0';
        nr/=10;
    }while(nr);

    while(k)
    {
        k--;
        putch(s[k]);
    }
}