Cod sursa(job #1470764)

Utilizator dorumusuroiFMI - Doru Musuroi dorumusuroi Data 12 august 2015 11:42:26
Problema Subsir crescator maximal Scor 70
Compilator c Status done
Runda Arhiva educationala Marime 0.75 kb
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100005
int n,i,j,maxt,aux, maxti = 1, a[MAXN], best[MAXN], pre[MAXN], sol[MAXN];
int main()
{
    FILE *in = fopen("scmax.in", "r");
    FILE *out = fopen("scmax.out", "w");
    fscanf(in,"%d",&n);
    for(i = 1; i <= n; i++)
        fscanf(in, "%d", a+i);
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j < i; j++)
            if(a[j] < a[i] && best[j] + 1 > best[i]) best[i] = best[j] + 1, pre[i] = j;
        if(best[i] > maxt ) maxt = best[i], maxti = i;
    }
    fprintf(out, "%d\n", best[maxti] + 1);
    i = 0;
    aux = maxti;
    while(maxti) sol[i++] = a[maxti], maxti = pre[maxti];
    for(i = best[aux] ; i >= 0; i--)
        fprintf(out,"%d ", sol[i]);
    return 0;
}