Cod sursa(job #271369)

Utilizator sory1806Sandu Sorina-Gabriela sory1806 Data 5 martie 2009 10:52:24
Problema Lupul Urias si Rau Scor 48
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include<stdio.h>
#define nmax 100010
long h[nmax], t[nmax], ti[nmax], d[nmax], ln[nmax], n, x, l, nh, tmax, nr, nt;
FILE *f, *g;

void ins(long lana)
{	long k, z;
	h[++nh]=lana;
	k=nh;
	while(h[k/2]<h[k] && k>1)
	{       z=h[k]; h[k]=h[k/2]; h[k/2]=z;
		k=k/2;
	}
}

void del(long k)
{	long z, m;
	z=h[k]; h[k]=h[nh]; h[nh]=z;
	nh--;
	while(( (h[2*k]>h[k] && 2*k<=nh) ||
		(h[2*k+1]>h[k] && 2*k+1<=nh) )	&& k<nh)
	{       if(2*k==nh)			m=2*k;
		else	if(h[2*k]>h[2*k+1])	m=2*k;
			else			m=2*k+1;
		z=h[m]; h[m]=h[k]; h[k]=z;
		k=m;
	}
}

void insbn(long x, long poz)
{       long i, p, u, mj;
	p=1; u=nt;
	while(p<=u)
	{       mj=(p+u)/2;
		if(x>t[mj])
			u=mj-1;
		else
			p=mj+1;
	}
	for(i=nt+1; i>p; i--)
	{	t[i]=t[i-1];
		ti[i]=ti[i-1];
	}
	t[p]=x;
	ti[p]=poz;
	nt++;
}


int main()
{       long i, j;
	f=fopen("lupu.in", "r");
	g=fopen("lupu.out", "w");
	fscanf(f, "%ld%ld%ld", &n, &x, &l);
	for(i=1; i<=n; i++)
	{	fscanf(f, "%ld%ld", &d[i], &ln[i]);
		if(x-d[i]>=0)
		{       insbn((x-d[i])/l+1, i);
			if((x-d[i])/l+1>tmax)
				tmax=(x-d[i])/l+1;
		}
	}
	i=1;
	for(j=tmax; j>=1; j--)
	{	while(t[i]==j && i<=nt)
		{	ins(ln[ti[i]]);
			i++;
		}
		if(nh>0)
		{	nr+=h[1];
			del(1);
		}
	}
	fprintf(g, "%ld\n", nr);
	fclose(g);
	return 0;
}