Cod sursa(job #438202)

Utilizator anaitsircVoicu Cristiana anaitsirc Data 10 aprilie 2010 16:06:30
Problema Gutui Scor 10
Compilator c Status done
Runda teme_upb Marime 1.08 kb
#include <stdlib.h>
#include <stdio.h>


typedef struct{
	int h;
	int g;
}gutuie;


int compare(const void *a,const void *b){
	gutuie * first=(gutuie *) a;
	gutuie * second=(gutuie *) b;
	return (second->g - first->g);
}

int main(){
gutuie sir[100000];
int i;
FILE *fp=fopen("gutui.in","r");
FILE *fp1=fopen("gutui.out","w");
char s[80];
fgets(s,80,fp);

char *sep=" ";
int N=atoi(strtok (s,sep));

int H=atoi(strtok (0,sep));

int U=atoi(strtok (0,sep));

int n=0;
int niv;
int * a= (int*) calloc (H/U+2, sizeof(int) );

while ( fgets(s,80,fp) != NULL ){
	sir[n].h=atoi(strtok (s,sep));
	sir[n].g=atoi(strtok (0,sep));
	
	niv=(H-sir[n].h)/U+1;
	a[niv]=1;
	n++;
}//end while

fclose (fp);
int g=0;
qsort(sir,N,sizeof(gutuie),compare);
int nr=0;
for(i=1;i<=H/U+1;i++)
	if (a[i]!=0) {nr++;
			a[i]=0;}
int max=0;
int nr1=0;
for( i=0; i<N;i++){
	if (nr1==nr) break;
	
	n=(H-sir[i].h)/U+1;
	 
	while ((a[n]!=0) && (n>0))
		n--;
	if (n>max) max=n;
	
	if (n>0) {
		a[n]=sir[i].g;
		nr1++;
		}
	
	
			
}

for (i=1; i<=max+1; i++){
	
	if (a[i]!=0)
		g=g+a[i];
	
	
}

	

fprintf(fp1,"%d",g);
fclose(fp1);
}