Cod sursa(job #800248)

Utilizator andreitaleanuAndrei Taleanu andreitaleanu Data 21 octombrie 2012 03:52:39
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <stdio.h>
#include <stdlib.h>
//#include <conio.h>

#define MAX 100000

int scmax(int v[], int n, FILE *f)
{
    int i, j, max, best[n], pre[n], lmax = 0, ind_max;
    
    best[0] = 1;
    for (i = 0; i < n; i++)
    {   for (j = 0, max = 0; j < i; j++)
        {   if (v[j] < v[i] && max < best[j])
            {  max = best[j];
               pre[i] = j;
            }
        }   
        best[i] = 1 + max;
        if (lmax < best[i])
        {  lmax = best[i];
           ind_max = i;
        }
    }
    
    int reconstruct[lmax], maxlen = lmax;
    fprintf(f, "%i\n", lmax);
    while (lmax != 0)
    {     reconstruct[--lmax] = v[ind_max];
          ind_max = pre[ind_max];
    }
    for (i = 0; i < maxlen; i++)
        fprintf(f, "%i ", reconstruct[i]);  
    return maxlen;
}

int main()
{
    FILE *f, *g;
    int n, v[MAX], i, max, p;
    
    f = fopen("scmax.in", "r");
    
    fscanf(f, "%i", &n);
    for (i = 0; i < n; ++i)
        fscanf(f, "%i", v+i);
    
    g = fopen("scmax.out", "w");
    max = scmax(v, n, g);

    return 0;
}