Cod sursa(job #440830)

Utilizator victor_u_roVictor Ungureanu victor_u_ro Data 12 aprilie 2010 16:10:36
Problema Gutui Scor 100
Compilator cpp Status done
Runda teme_upb Marime 1.26 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define NMAX 100024

struct gutuie {
	long t, g;
	
	bool operator<(gutuie x) const {
		return (g < x.g);
	}
};

long n, gmax;
gutuie a[NMAX];

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

	freopen("gutui.in", "rt", stdin);
	
	scanf("%ld %ld %ld", &n, &h, &u);
	nn = 0;
	for (i = 0; i < n; i++) {
		scanf("%ld %ld", &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 lt, ng, i, hsize;
	gutuie b[NMAX];

	sort(a, a + n, compt);
	
	ng = 0;
	lt = a[0].t;
	gmax = 0;
	hsize = 0;
	for (i = 0; i < n; i++) {
		if (lt != a[i].t) {
			ng += lt - a[i].t;
			lt = a[i].t;
			
			for (; ng > 0 && hsize > 0; ng--, hsize--) {
				pop_heap(b, b + hsize);
				gmax += b[hsize - 1].g;
			}
			ng = 0;
		}
	
		b[hsize++] = a[i];
		push_heap(b, b + hsize);
	}

	ng += lt - a[i].t + 1;
	ng += a[i].t;
	for (; ng > 0 && hsize > 0; ng--, hsize--) {
		pop_heap(b, b + hsize);
		gmax += b[hsize - 1].g;
	}
}

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

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

	return 0;
}