Cod sursa(job #439870)

Utilizator maria.eniEni Maria-Adina maria.eni Data 11 aprilie 2010 20:06:21
Problema Gutui Scor 10
Compilator c Status done
Runda teme_upb Marime 1.77 kb

//#include <stdlib.h>
#include <stdio.h>


int main()
{

	long int h[10000], g[10000];
	long int i, j, n, hmax, up, count, put, aux, sum=0;
	
	FILE* fin;
	FILE* fout;
	
	fin = fopen ("gutui.in", "r");
	fout = fopen ("gutui.out", "w");
	
	fscanf (fin, "%li %li %li", &n, &hmax, &up);

	for ( i=0; i<n; i++)
		fscanf ( fin, "%li %li", &h[i], &g[i] );
	
	int up1=up;
	count=0;
	while ( up1 / 10)
	{
		up1 = up1 / 10;
		count= count +1;
	}

	put=1;
	while( count != 0)
	{
		put = put * 10;
		count = count - 1 ;
	}
	
	for( i=0; i<n; i++)
		for( j=i+1; j<n; j++)
	{
		if ( (h[i] / put) <= (h[j] / put) )
		{
			aux = h[i];
			h[i] = h[j];
			h[j] = aux;
			
			aux = g[i];
			g[i] = g[j];
			g[j] = aux;
		}
		
	}

	for( i=0; i<n-1;i++)
    {
         for ( j=i+1; j<n;j++)
         {
             if( (h[i] / put) == (h[j] / put) )
             {
                        while( g[i] < g[j] )
                        {
                        aux = h[i];
						h[i] = h[j];
						h[j] = aux;
			
						aux = g[i];
						g[i] = g[j];
						g[j] = aux;
						}
             }
             if( put == 1)
             {
             	if ( g[i] < g[j] && ( (h[i]+up) %10 == (h[j]+up) %10) )
             		break;
					}
             	
			}
    }
 /*   
    for( i=0; i<n; i++)
		fprintf(fout, "%li ", h[i]);
		
	fprintf(fout, "\n");	
	for( i=0; i<n; i++)
		fprintf(fout, "%li ", g[i]);
	*/	
    
    for( i=0; i<n; i++)
    {
    	if( h[i] <= hmax)
    	{
    		sum = sum + g[i];
    		for( j=i+1; j<n; j++)
    			h[j] = h[j] + up;
    	//	fprintf(fout, "--%li --", g[i]);
		}

    	
	}
	
	fprintf(fout, "%li", sum);
	/*
	fprintf(fout, "\n");
	
	for( i=0; i<n; i++)
		fprintf(fout, "%li ", h[i]);
		
	fprintf(fout, "\n");	
	for( i=0; i<n; i++)
		fprintf(fout, "%li ", g[i]);
	*/

	fclose(fin);
	fclose(fout);

return 0;
}