Cod sursa(job #463067)

Utilizator simona.poilincaSimona Poilinca simona.poilinca Data 14 iunie 2010 14:53:07
Problema Gutui Scor 10
Compilator c Status done
Runda teme_upb Marime 1.46 kb
#include<stdio.h>
#include<stdlib.h>


//functie ce determina elementul maxim dintr-o portiune din vector
long int max_greutate (long int *a, long int n, long int k)
{
long int i, max=0;
for (i=n-k; i<n; i++)
	if (a[i] > max)
		max=a[i];
return max;
}
//functie ce determina elementul maxim dintr-o portiune din vector


int main()
{
long int n, h, u, *height, *weight, i, j, aux, nr_gutui_ce_dispar, greutate=0;
FILE *fin, *fout;


//citirea datelor de intrare
fin=fopen ("gutui.in", "r");
fscanf (fin, "%li %li %li", &n, &h, &u);
height=malloc ( n*sizeof (long int));
weight=malloc ( n*sizeof (long int));
for (i=0; i<n; i++)
	fscanf (fin, "%li %li", &height[i], &weight[i]);
//citirea datelor de intrare


//rezolvarea problemei
for (i=0; i<n-1; i++)
	for (j=i+1; j<n; j++)
		if (height[i]>height[j])
			{
			aux=height[i];
			height[i]=height[j];
			height[j]=aux;
			aux=weight[i];
			weight[i]=weight[j];
			weight[j]=aux;
			}
while (n>0)
	{
	nr_gutui_ce_dispar=0;
	for (i=0; i<n; i++)
		if (height[i] > h-u)
			nr_gutui_ce_dispar++;
	if (nr_gutui_ce_dispar != 0)
		{
		greutate+=max_greutate (weight, n, nr_gutui_ce_dispar);
		n-=nr_gutui_ce_dispar;
		}
	else
		{
		greutate+=weight[n-1];
		n--;
		}
	for (i=0; i<n; i++)
		height[i]+=u;
	}
//rezolvarea problemei


//scrierea solutiei in fisierul de iesire
fout=fopen ("gutui.out", "w");
fprintf (fout, "%li\n", greutate);
//scrierea solutiei in fisierul de iesire


fclose (fin);
fclose (fout);
free (height);
free (weight);
return 0;
}