Cod sursa(job #434126)

Utilizator ShmazZamfirache Virgil Shmaz Data 5 aprilie 2010 04:54:11
Problema Gutui Scor 0
Compilator cpp Status done
Runda teme_upb Marime 4.11 kb
/* Zamfirache Virgil - 323CC
 * PA - TEMA 1 - PROBLEMA 2*/

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

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 ] ;8
		}
		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 && weight[j - 1] < weight[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 )
{   
	vector<int> asd;
	FILE *out ;
 
    out =  fopen ( "gutui.out", "w" );
    
    int i , j, nr_alese = 0 ,  w_max = 0;
    int *aux;
    
    aux = ( int *  ) calloc ( sizeof ( int ) , 1 + (( H - HMIN ) / U)  )  ; 
	   
    insertionSort ( height, weight, N );
	int ax = 1 + (( H - HMIN ) / U) ;
/*	for ( i = 0 ; i < N ; i ++ ) {
			cout<<"h: "<<height[i]<<" w:" << weight[i]<<endl;
	}*/
	/*cout<<"AX: "<<ax;
	for ( i = 0 ; i < N ; i++ ) {

		for ( j = ( H - height[ i ] ) / U  ; j >= 0 ; j --  ) {

			if ( aux [ j ] == 0 ) {
			    aux[ j ]  = weight[ i ] ;
				w_max += weight[ i ] ;  
				nr_alese++ ; 	  
				break;
            }
   
			   
		}
		if ( nr_alese == ax)
		   break ;
	
	}*/
			
//	cout<<"w_max: "<<w_max<<endl;
	fprintf ( out , "%d", w_max);
    fclose ( out ) ;
	
   // system("pause");	
}


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