Cod sursa(job #438952)

Utilizator ralu_k_sSocea Raluca ralu_k_s Data 11 aprilie 2010 06:29:48
Problema Gutui Scor 0
Compilator c Status done
Runda teme_upb Marime 2.08 kb
#include<stdio.h>
#include<stdlib.h>
unsigned long int *h, *g, U;

int compare (const void * a, const void * b)
{
	int i = *(int*)a, j = *(int *)b;
	if( *(g+i) < *(g+j) )
		return 1;
	if( *(g+i) == *(g+j) )
	{	if( *(h+i) < *(h+j) )
			return 1;
		if( *(h+i) == *(h+j) )
			return 0;
	}
	return -1;	
	
}


int main()
{	
	unsigned long int H,  Gmax=0, gcrt, hcrt;
	int N;

	// Citire
	FILE *fin;
	fin = fopen("gutui.in","rt");
	fscanf(fin,"%i %lu %lu",&N, &H, &U);
	
	// Alocare vectori de dimensiune N (greutatile si inaltimile gutuielor)	
	g = (unsigned long int *) calloc(N, sizeof(unsigned long int));
	h = (unsigned long int *) calloc(N, sizeof(unsigned long int));


	int *ord = (int *) calloc(N, sizeof(int));
	int i,j;
	for(i=0;i<N;i++)
	{	fscanf(fin,"%lu %lu",h+i, g+i);
		*(ord+i) = i;
	}

	fclose(fin);
	qsort(ord, N, sizeof(int), compare);	
		
	for(i=0;i<N;i++)
		printf("%i ", *(ord+i)+1 );//raspuns: 233000000
	printf("\n");

	int *v = (int *) calloc(N, sizeof(int));
	for(i=0;i<N;i++)
		*(v+i) = -1;
	
	int niv=0, stop = -1;
	for(i=0; i<N;i++)
	{	gcrt = *(g+ *(ord+i));
		hcrt = *(h+ *(ord+i));
		if(hcrt > H)
			*(v+i) = -2;
		else
			if( H- niv*U < hcrt)
			{		
				if(stop == -1)
					stop = i;
				else continue;
			}
			else
			{	*(v+i) = (H-niv*U -hcrt)/U;
				Gmax += gcrt;
				niv++;	
			}
		
	}
	for(i=0;i<N;i++)
		printf("%i ", *(v+i) );
	printf("\n%lu\n", Gmax);
	printf("\nniv: %i\n", niv);
	printf("\nstop: %i\n", stop);
	int k , re, gasit;

	for(i=stop; i< N; i++)
	{	
		while(*(v+i)!= -1 && i<N)
			i++;
		if(i==N)
			break;
		hcrt = *(h+ *(ord+i));		 
		
		
		j = N-1;
		re = niv;
		gasit = 1;
		while( *(v+j)!=0 && (H - re*U)< hcrt && j>=0)
		{
			if( *(v+j)>0)
			{	re--;
				*(v+j) = *(v+j) - 1;
			}
			
			if(!j) 
			{	gasit = 0;
	
				break;
			}
			j--;
			if(*(v+j)==0)
				gasit=0;	
		}
		
				
		if(j>=0 && gasit )
		{	
			Gmax += *(g+ *(ord+i));
			*(v+i) = 0;
			niv++;
		}
		else
		for(k = j+1; k<N;k++)
				if(*(v+k)>=0)
					(*(v+k))++;
		
	}
	
		
	
	//Afisare
	FILE *fout;

	fout = fopen("gutui.out","wt");
	fprintf(fout,"%lu", Gmax);
	printf("\n%lu\n", Gmax);
	fclose(fout);
	return 0;
}