Cod sursa(job #440692)

Utilizator victor_u_roVictor Ungureanu victor_u_ro Data 12 aprilie 2010 13:09:05
Problema Gutui Scor 90
Compilator cpp Status done
Runda teme_upb Marime 1.38 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define NMAX 100024

struct gutuie {
	long long t, g;

	bool compg(gutuie x) const {
		return (g < x.g);
	}
	
	bool operator<(gutuie x) const {
		return compg(x);
	}
};

long long n, gmax;
gutuie a[NMAX];

void cit() {
	long long h, u;
	long long i, nn;

	freopen("gutui.in", "rt", stdin);
	
	scanf("%lld %lld %lld", &n, &h, &u);
	nn = 0;
	for (i = 0; i < n; i++) {
		scanf("%lld %lld", &a[nn].t, &a[nn].g);
		
		if (a[nn].t <= h) {
			a[nn].t = (h - a[nn].t) / u;
			nn++;
		}
		
	}
	n = nn;
	
	fclose(stdin);
}

bool compt(gutuie x, gutuie y) {	
	if (x.t == y.t) {
		return (x.g > y.g);
	}
	
	return (x.t > y.t);
}

void rez() {
	long long lt, ng, i, hsize;
	gutuie b[NMAX];

	sort(a, a + n, compt);
	
	/*for (i = 0; i < n; i++)
		printf("%ld/%ld ", a[i].t, a[i].g);
	printf("\n");*/
	
	ng = 0;
	lt = a[0].t;
	gmax = a[0].g;
	hsize = 0;
	for (i = 1; i < n; i++) {
		ng += lt - a[i].t;
		lt = a[i].t;
		
		if (i == n - 1)
			ng += a[i].t;
		
		b[hsize++] = a[i];
		push_heap(b, b + hsize);

		for (; ng > 0 && hsize > 0; ng--, hsize--) {
			pop_heap(b, b + hsize);
			gmax += b[hsize - 1].g;
		}
		
		/*for (int j = 0; j < hsize; j++)
			printf("%ld/%ld ", b[j].g, b[j].t);
		printf("\n");*/
	}
}

void afis() {
	freopen("gutui.out", "wt", stdout);
	
	printf("%lld\n", gmax);
	
	fclose(stdout);
}

int main() {
	cit();
	rez();
	afis();

	return 0;
}