Cod sursa(job #1349451)

Utilizator IonSebastianIon Sebastian IonSebastian Data 20 februarie 2015 11:10:58
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>

using namespace std;

const int MAX_N = 100000, LOG = 17;

FILE *in, *out;

int v[MAX_N+1], u[MAX_N+1], pred[MAX_N+1], sol[MAX_N+1];

int lung;

int bsearch(int val)
{
    int i = 0, pas = 1 << LOG;
    while(pas != 0)
    {
        if(i+pas <= lung && v[u[i+pas]] < val)
            i += pas;
        pas /= 2;
    }
    return i;
}

int main()
{
    in = fopen("scmax.in", "r");
    out = fopen("scmax.out","w");
    int n;
    int i, j;
    fscanf(in, "%d", &n);
    for(i = 1; i <= n; i++)
        fscanf(in, "%d", &v[i]);
    u[1] = 1;
    lung = 1;
    for(i = 2; i <= n; i++)
    {
        j = bsearch(v[i]);
        if(j == lung)
            lung++;
        pred[i] = u[j];
        u[j+1] = v[i];
    }
    fprintf(out, "%d\n", lung);
    j = lung;
    for(i = u[lung]; i > 0; i = pred[i])
        sol[j--] = v[i];
    for(i = 1; i <= lung; i++)
        fprintf(out, "%d ", sol[i]);
    fclose(in);
    fclose(out);
    return 0;
}