Cod sursa(job #430450)

Utilizator ShmazZamfirache Virgil Shmaz Data 31 martie 2010 01:07:26
Problema Gutui Scor 10
Compilator cpp Status done
Runda teme_upb Marime 4.66 kb
/* Zamfirache Virgil - 323CC
 * PA - TEMA 1 - PROBLEMA 2*/

#include <iostream>
#include <stdlib.h>

using namespace std;


int max2( int a, int b  )
{
    int max ;
    
    max = ( a > b ) ? a : b ;
    
    return max;    
    
}

void free_matrix( int n, int **a) {
	int i;
	for ( i = 0 ; i < n ; i++) {
		free( a[ i ] );
	}
	free( a );
}

/*void gutui( int * height, int *weight, int N, int H, int U , int HMIN)
{	
	FILE *out ;

    out =  fopen ( "gutui.out", "w" );
    int i , h ;
    int *A , *B;
    A = (int * ) calloc ( H + U + HMIN + 1 , sizeof ( int * ));
    B = (int * ) calloc ( H + U + HMIN + 1 , sizeof ( int * ));
    
    //for ( h = 10 ; h <= H  ; h ++ ){
		
    for ( i = 1 ; i <= N ; i ++) {
        for ( int p = 0 ; p <= H + U + HMIN ; p++ ){
       	    A[ p ] = B[ p ] ;
		}
		for ( h = height [ i - 1 ] ; h <= H + U + HMIN ; h ++ ){
			if ( A [ h - U ] +  weight[ i - 1] > A [ h ] )
   			    B[ h ] = A[ h -  U ] + weight[ i - 1 ] ;
		}
    }
   	for ( int p = 0 ; p <= H + U + HMIN; p++ ){ 
    	cout <<"p:"<<p<<":"<< B[p]<<endl;
    }
		//	cout<<endl<<" H : "<< height[ i - 1 ]<<" W:" <<weight [ i - 1] <<endl;
		//	aux[ h ][ i ] = ( height[ i - 1 ] <= h ) ? max2 ( aux [ h ][ i - 1 ], aux [ h - 10 ][ i - 1 ] + weight [ i - 1  ] ) : aux [ h ][ i - 1 ];
		//	if ( h[ i ] < = H ) {
  		//	cout <<"a["<<h<<"]["<<i<<"]="<<aux[h][i]<<" "<<endl;
           // }

	//	cout<<endl;
	//	system("pause");
	fprintf ( out , "%d", B[H]);
    fclose ( out ) ;
    system("pause");
}*/

void insertionSort( int * height, int *weight, int N ) {

      int i, j, tmp;

      for (i = 1; i < N; i++) {

            j = i;

            while (j > 0 && height[j - 1] >= height[j]) {

                  tmp = height[j];
                  height[j] = height[j - 1];
                  height[j - 1] = tmp;

                  tmp = weight[j];
                  weight[j] = weight[j - 1];
                  weight[j - 1] = tmp;

                  j--;

            }

      }

}

void gutui( int * height, int *weight, int N, int H, int U, int HMIN )
{   
	FILE *out ;

    out =  fopen ( "gutui.out", "w" );
    
    int i , h ;
    int **aux;
    aux = (int ** ) calloc ( ( H + 1) , sizeof ( int * ));
	for ( i = 0 ; i <= H  ; i ++ ) {
	    aux[ i ] = ( int * ) calloc ( ( N + 1 ) , sizeof ( int ) );
	} 
	   
    insertionSort ( height, weight, N );

//	for ( i = 0 ; i < N ; i ++ ) {
//			cout<<"h: "<<height[i]<<" w:" << weight[i]<<endl;
//	}

    HMIN = ((int )(HMIN / U )) * U;
    //cout<<"HMIN: "<<HMIN<<endl;
    
    for ( h = HMIN  ; h <= H  ; h ++ ){
		
		for ( i = 1 ; i <= N ; i ++ ) {

			if ( height[ i - 1 ] <= h   )                 
                aux[ h ][ i ] = max2 ( aux[ h ][ i - 1 ], aux[ h - U ][ i - 1 ] + weight[ i - 1 ] );
            else 
                aux[ h ][ i ] = aux [ h ][ i - 1 ];
                
			//aux[ h ][ i ] = ( height[ i - 1 ] <= h && height[i - 1] > h-10 ) ? max2 ( aux [ h ][ i - 1 ] , aux [ h - 10 ][ i - 1 ] + weight [ i - 1  ] ) : aux [ h ][ i - 1 ];
			//aux[ h ][ i ] = ( height[ i - 1 ] <= h - 10 ) ? max2 ( aux [ h ][ i - 1 ] + weight[i-1] , aux [ h - 10 ][ i - 1 ] + weight [ i - 1  ] ) : aux [ h ][ i - 1 ];
		//	if ( h[ i ] < = H ) {
  		//	cout <<"a["<<h<<"]["<<i<<"]="<<aux[h][i]<<" "<<endl;
  			
           // }
           
		}
	//	cout<<endl;
	//	system("pause");	

	}
	
	//cout<< aux[H][N];
	fprintf ( out , "%d", aux[H][N]);
    fclose ( out ) ;
	
   // system("pause");	
}


void read( char *fname , int **height, int **weight , int *N, int *H, int *U , int *HMIN)
{
    FILE *in ;
    int i, j;
    in =  fopen ( fname, "r" );
    *HMIN = INT_MAX ;
    fscanf(in, "%d", N);
    fscanf(in, "%d", H);
    fscanf(in, "%d", U);
	
	// elementele matricei A
	*height = (int*)calloc(( *N ) , sizeof(int*));
	*weight = (int*)calloc(( *N ) , sizeof(int*));
	for ( i = 0; i < *N ; i++) {

			fscanf(in, "%d", &( ( *height )[ i ] ));
			fscanf(in, "%d", &( ( *weight )[ i ] ));
			if ( (*height)[i] < *HMIN ){
			   *HMIN = (*height)[ i ];
            }
	}
	//cout<<*HMIN;
	fclose ( in );
    
}

int main()
{
    int *height, *weight;
    /*N (numarul de gutui din copac), H (inaltimea maxima la care ajunge Gigel) si 
	U (cu cat se ridica crengile copacului dupa culegerea unei gutui).*/
    int N, H, U, HMIN;
    char in_name[] = "gutui.in";
	
    read( in_name, &height, &weight, &N, &H, &U, &HMIN);
    
	/*for ( i = 1; i <= N ; i++) {
			cout<< height[ i - 1  ]<< " " << weight[ i -1  ]  ;
				cout<<endl;
	}	*/
			
			
    gutui( height, weight, N, H, U, HMIN );
	//system("pause");
    return 0;
}