Cod sursa(job #434749)

Utilizator theonlyonealina sincu theonlyone Data 6 aprilie 2010 14:54:24
Problema Gutui Scor 10
Compilator c Status done
Runda teme_upb Marime 2.12 kb
#include<stdlib.h>
#include<stdio.h>
typedef struct{
		int height;
		int weight;
		}pom;

int main(int argc, char * argv[]){
	int n,u,h,aux1, aux2;
	int i,j,x,ind,max,sum = 0;
	FILE * fis = fopen("gutui.in","r");
	if (fis == NULL) printf("Couldn't open file\n");
	//char * p =(char*)strtok(argv[1],".");
	//strcpy(p+strlen(p),"out.txt");
	FILE * out = fopen("gutui.out","w");

	fscanf(fis,"%d",&n);
	fscanf(fis,"%d",&h);
	fscanf(fis,"%d",&u);
	
	pom * v = (pom*)malloc(n*sizeof(pom));

	for (i = 0; i<n; i++){
		fscanf(fis,"%d %d",&v[i].height,&v[i].weight);
		}
	int ord ;	
	do 	{
		ord  = 0;
		for (i = 0; i<n-1; i++)
			if (v[i].height < v[i+1].height){
			   				 aux1 = v[i+1].height;
							 aux2 = v[i+1].weight;
							 v[i+1].height = v[i].height;
							 v[i+1].weight = v[i].weight;
							 v[i].height = aux1;
							 v[i].weight = aux2;
							 ord  = 1;
							 }
		 else if ((v[i].height == v[i+1].height) && (v[i].weight < v[i+1].weight)){
		 	  aux1 = v[i+1].height;
							 aux2 = v[i+1].weight;
							 v[i+1].height = v[i].height;
							 v[i+1].weight = v[i].weight;
							 v[i].height = aux1;
							 v[i].weight = aux2;
							 ord  = 1;
		 	  }
							 
			   
	}while (ord == 1);
	x = 0;
    sum = 0;
	for (i = 0; i<n; i++){
		
		if (v[i].height + u*(x+1) > h){
		   			//	printf("aici %d\n",v[i].height);
		   				max  = 0; j = i;
		   				while ((v[j].height + u *(x+1)> h) && (j<n)){
							  if (max < v[j].weight){
							  	 	  max = v[j].weight;
							  	 	  ind = j;
		  	 	              }
                              j++;
  						}
  						sum = sum + v[ind].weight;
  						x++;
  						i = j - 1;
  					//	printf("i=%d\n",i);
	    }
	    else{
			 //printf("i=%d x=%d h=%d h+x=%d\n",i,x,v[i].height,v[i].height+u*x);
			 if (v[i].height + u * x <= h){
			   //printf("aici2");
			 	sum = sum + v[i].weight;
			 	x++;
				}
	    }
		   				
	}
	
	//for (i = 0; i<n; i++)
	//	printf("%d %d\n",v[i].height,v[i].weight);
	//printf("%d\n",sum);
	fprintf(out,"%d",sum);
	
	fclose(out);
	fclose(fis);
	getchar();
	getchar();
	return 0;
	
}