Cod sursa(job #1465110)

Utilizator om6gaLungu Adrian om6ga Data 26 iulie 2015 15:20:34
Problema Subsir crescator maximal Scor 70
Compilator c Status done
Runda Arhiva educationala Marime 1.49 kb
#include <stdio.h>
#include <string.h>
 
#define nmax 100005
#define size 1500000
 
int n, a[nmax], P[nmax], prev[nmax], p[nmax], i, j, len, max, imax;
char string[size];
 
int main()
{
    FILE *in, *out;
    in =  freopen("scmax.in", "r", stdin);
    out = freopen("scmax.out", "w", stdout);
     
    fscanf(in, "%d\n", &n);
     
    fgets(string, size, in);
     
    i = 1;
    len = strlen(string);
    for (j = 0; j < len; j++)
    {
        if (string[j] == ' ')
            continue;
        while (string[j] >= '0' && string[j] <= '9')
            a[i] = a[i]*10 + string[j++] - '0';
        i++;
    }
     
    for (i = 1; i <= n; i++)
    {
        P[i] = 1;
        prev[i] = -1;    
    }
     
    for (j = 2; j <= n; j++)
    {
        int m = 0;
        for (i = 1; i < j; i++)
            if (a[i] < a[j])
                if (m < 1 + P[i])
                {
                    m = 1 + P[i];
                    prev[j] = i;
                    if (m > max)
                    {
                        max = m;
                        imax = j;
                        break;      
                    }
                }
                
        P[j] = (m > P[j] ? m : P[j]);
    }
    i = 1;
    while (imax > 0)
    {
         p[i++] = a[imax];
         imax = prev[imax];
    }
     
    printf("%d\n", max);
    for (i = max; i >= 1; i--)
        printf("%d ", p[i]);
     
    fclose(in);
    fclose(out);
    return 0;   
}