Cod sursa(job #434752)

Utilizator adriana89Adriana adriana89 Data 6 aprilie 2010 14:58:21
Problema Gutui Scor 10
Compilator c Status done
Runda teme_upb Marime 1.39 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long int N ;
int H , U;

typedef struct gutui
		{
		int h;
		int g;
		int nivel;
		} GUTUI;
GUTUI *G;
void citire()
{
FILE *f = fopen ( "gutui.in" , "r" ) ;
fscanf( f , "%ld%d%d", &N , &H , &U) ;
G = ( GUTUI * ) malloc ( N * sizeof ( GUTUI ) ) ;
int i;
for ( i = 0 ; i < N ; i++ )
	{
	fscanf ( f , "%d%d" , &G[i].h , &G[i].g );
	G[i].nivel = H / U - G[i].h / U;	
	}
fclose ( f );
}
int verifica()
{
int i;
for ( i = 0 ; i < N ; i++ )
	if ( G[i].nivel != 0 ) return 1;
return 0;
}
void rezolva()
{
FILE *f = fopen ( "gutui.out" , "w" ) ;
int suma = 0;
int i , j , aux;
for ( i = 0 ; i < N ; i++)
	for ( j = 0 ; j < N ; j++ )
		{
		if ( G[i].nivel < G[j].nivel )
			{			
			aux = G[i].nivel;
			G[i].nivel = G[j].nivel;
			G[j].nivel = aux;
			aux = G[i].h;
			G[i].h = G[j].h;
			G[j].h = aux;
			aux = G[i].g;
			G[i].g = G[j].g;
			G[j].g = aux;
			}
		if ( G[i].nivel == G[j].nivel && G[i].g > G[j].g )
			{			
			aux = G[i].nivel;
			G[i].nivel = G[j].nivel;
			G[j].nivel = aux;
			aux = G[i].h;
			G[i].h = G[j].h;
			G[j].h = aux;
			aux = G[i].g;
			G[i].g = G[j].g;
			G[j].g = aux;
			}				
		}

while ( verifica ( ) )
	{
	i = 0;
	while ( G[i].nivel <= 0 )
		i++;
	suma = suma + G[i].g;
	for ( i = 0 ; i < N ; i++)
		if ( G[i].nivel != 0 )
			G[i].nivel --;
	}
fprintf ( f , "%d\n" , suma );
fclose(f);
}

int main()
{
citire();
rezolva();
return 0;
}