Cod sursa(job #429430)

Utilizator ShmazZamfirache Virgil Shmaz Data 30 martie 2010 09:48:04
Problema Gutui Scor 20
Compilator cpp Status done
Runda teme_upb Marime 4.57 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 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 ) );
	} 
	
    int aux1;
    bool gata = true ;
   
    while ( gata ) {
		  gata = false ;
		  
		  for ( i = 0 ; i < N - 1; i ++ ){
		  	  if ( height[ i ] > height[ i + 1 ] ) {
			  	   
  	  	          aux1 = height[ i + 1 ];
  	  	          height[ i + 1 ] = height[ i ] ;
  	  	          height[ i ] = aux1 ;
  	  	          
  	  	          aux1 = weight[ i + 1 ];
  	  	          weight[ i + 1 ] = weight[ i ] ;
  	  	          weight[ i ] = aux1 ;  
				  		  
				  gata = true ;		  
	          }	  	          
		  
		  }
    }
    
    for ( h = 0 ; h <= H  ; h = h + U ){
		
		for ( i = 1 ; i <= N ; i ++ ) {
		//	cout<<endl<<" H : "<< height[ i - 1 ]<<" W:" <<weight [ i - 1] <<endl;//+ weight [ i - 1  ]
			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"); 
	}
	fprintf ( out , "%d", aux[H][N]);
    fclose ( out ) ;
    //system("pause");
	//cout<< aux[H][N];
}


void read( char *fname , int **height, int **weight , int *N, int *H, int *U , int *HMIN)
{
    FILE *in ;
    int i, j;
    char *str;
    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;
    int i, j;
    /*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;
}