Cod sursa(job #434590)

Utilizator violeta.marinVioleta Marin violeta.marin Data 6 aprilie 2010 04:06:28
Problema Gutui Scor 10
Compilator c Status done
Runda teme_upb Marime 1.37 kb
#include <stdio.h>
#include <stdlib.h>


struct gut{
	unsigned int inalt;	
	unsigned int greu;
	  };

int compare (const void * a, const void * b)
{
  //return ( *(int*)a - *(int*)b );
	return -((*(struct gut*)a).inalt - (*(struct gut*)b).inalt); 	
}

	
int main(){
	int i, j;
	FILE *f = fopen("gutui.in","r");
	unsigned int n = 0,h = 0 ,u = 0, max = 0, gr;
	fscanf(f, "%d", &n);
	fscanf(f, "%d", &h);
	fscanf(f, "%d", &u);
	
	struct gut *gutui = (struct gut *)malloc(n * sizeof(struct gut));
	for (i = 0; i < n; i++)
		{
		fscanf(f, "%u", &gutui[i].inalt);
		fscanf(f, "%u", &gutui[i].greu);
		}

	qsort(gutui, n, sizeof(struct gut), compare);
	/*for (i = 0; i < n; i++)
		{
		printf("%u ", gutui[i].inalt);
		printf("%u\n", gutui[i].greu);
		}*/
	fclose(f);

	i = 0;
	j = 0;
	gr = 0;
	int pas = 1, crescut = 0;
	for ( i = 0; i < n; i++)
		{
		//gr = gutui[i].greu;
		if ((gutui[i].inalt + pas * u) < h)	{
						pas++;
						//printf("%d\n", pas);
						if (crescut == 1)
						max = max + gr;
						else
						max = max + gutui[i].greu;
						gr = 0;
						crescut = 0;
							}
		if (gutui[i].greu > gr)
				{ 
				gr = gutui[i].greu;
				crescut = 1;
				}
		if ( i == (n - 1)) {
				  max = max + gr;
				  break;
				 }		
		//printf("%d\n", gr);
		}
		
		 	
		//printf("%d\n", max);	
		     	
	//printf("%d\n", max);
	f = fopen("gutui.out","w");
	fprintf(f,"%d\n", max);
	fclose(f);

return 0;
}