Cod sursa(job #438035)

Utilizator silviu.novitchiNOVITCHI Silviu silviu.novitchi Data 10 aprilie 2010 13:58:36
Problema Gutui Scor 10
Compilator c Status done
Runda teme_upb Marime 1.43 kb
#include <stdlib.h>
#include <stdio.h>
unsigned long hmax, inc;
int nr;
typedef struct Gutui
{
	unsigned long h, w, k;
} *gutui;

void ord(gutui *pom)
{
	int i,j;
	gutui gut;
	for (i = 0; i < nr; i++)
		for (j = 0; j < nr; j++)
		{
			if (pom[i]->h > pom[j]->h) 
			{
				gut = pom[i];
				pom[i] = pom[j];
				pom[j] = gut;
			}
			else 
			if (pom[i]->h == pom[j]->h) 
			{
				if (pom[i]->w > pom[j]->w)
				{
				gut = pom[i];
				pom[i] = pom[j];
				pom[j] = gut;
				}
			}
		}
}

int culege(gutui *pom)
{
	unsigned long l = 0, rez = 0;
	int i;
	
	for ( i = 0; i < nr; i++)
	{
		if (pom[i]->k > l)
		{
			rez += pom[i]->w;
			l++;
		}
	}

return rez;
}

int main()
{
	FILE *f, *g;
	gutui *pom;
	unsigned long rez;
	
	int i , j;
		
	f = fopen("gutui.in", "r");
	g = fopen("gutui.out", "w");
	fscanf(f,"%d %lu %lu", &nr, &hmax, &inc);
	pom = (gutui*) malloc (nr * sizeof(gutui));
	for (i = 0; i < nr; i++)
	{
		pom[i] = (gutui) malloc (sizeof(struct Gutui));
		fscanf(f, "%lu %lu", &pom[i]->h, &pom[i]->w);
		if (pom[i]->h > hmax)
		{
			pom[i]->k = 0;
		}
		else 
		{
			pom[i]->k = (hmax - pom[i]->h) / inc + 1;
		}
	}	
	
	ord(pom);
	rez = culege(pom);
	fprintf(g,"%lu", rez);
	
	
	
	
	
/*	for (i = 0; i < nr; i++)
	{
		printf("%lu %lu %lu\n", pom[i]->h, pom[i]->w, pom[i]->k);
	}
*/	
	//printf("%d %lu %lu",nr, hmax, inc);
fclose(f);
fclose(g);
return 0;
}