Cod sursa(job #1221083)

Utilizator tedyy91razvan teslaru tedyy91 Data 19 august 2014 13:29:11
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.86 kb
#include "stdio.h"
#include "stdlib.h"

#define NMAX 100003


int n , a[NMAX] , *lis , imax = 0 , res[NMAX];


void read()
{
    FILE *hFileIn = fopen( "scmax.in" , "r" );     //open in file  
    if( hFileIn == NULL )
    {
       printf("[error] could not open input file \nexiting \n");        
    } 

    fscanf(hFileIn , "%d" , &n); //read n

    for(int i = 0 ; i < n ; i++)
    {
        fscanf(hFileIn , "%d" , &a[i]); //read n
    }

    fclose( hFileIn );


}

void LIS()
{
    int i, j  ;
    imax = 0 ;

    lis = (int*) malloc( sizeof( int ) * n) ; //aloc mem for lis

    for( i=0 ; i < n ; i++) //init
        lis[i] = 1 ;


    for(int i=1 ; i<n ; i++)
    {
        for( int j=0; j<i ; j++)
        {
            if(a[j] < a[i] && lis[i] < lis[j] + 1)
            {
                lis[i] = lis [j] + 1;
                if( lis[i] > lis[imax] )
                {
                    imax = i ;                    
                }
            }
        } 
    }
        
}

void constr()
{
    int i , j, poz, nr, len;
    
    poz = imax;
    //nr = a[imax];
    //len = lis[poz];

    res[ lis[poz] -1 ] = a[poz];
    for(i=poz -1 ; i >= 0 ; i--)
    {
        if( lis[i]+1 == lis[poz] && a[i] < a[poz] )
        {            
            res[lis[i] -1] = a[i];
            poz = i;
        }        
    }
}
void write()
{
    FILE *hFileOut = fopen( "scmax.out" , "w" );   //open out file
    if( hFileOut == NULL )
    {
       printf("[error] could not create output file \nexiting \n");        
    }

    //write res
    fprintf( hFileOut , "%d\n" , lis[imax] );
    for(int i=0; i<lis[imax] ; i++)
    {
        fprintf( hFileOut , "%d " , res[i] );
    }

    fclose( hFileOut );
}
int main()
{
    
    read();

    LIS();

    constr();

    write();
  
 
	return 0;
}