Cod sursa(job #436976)

Utilizator carbonixVictor Carbune carbonix Data 9 aprilie 2010 02:27:04
Problema Gutui Scor 0
Compilator cpp Status done
Runda teme_upb Marime 1.38 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#define N 100000

using namespace std;

typedef struct {
	unsigned long w, h, pos;
} quince;

int n, hmax, u;
vector<quince> v, heap;

void read() {
	int i;

	scanf("%d %d %d", &n, &hmax, &u);

	for ( i = 0; i < n; i++ ) {
		quince e;
		scanf("%d %d", &e.h, &e.w);
		e.pos = (hmax - e.h)/u + 1;

		v.insert(v.end(), e);
	}
}

void print() {
	unsigned int i;
	
	for ( i = 0; i < v.size(); i++ )
		printf("%d %d %d\n", v[i].h, v[i].w, v[i].pos);
}

bool quinceCmpWeight (const quince q1, const quince q2) {
	if (q1.w < q2.w)
		return true;
	return false;
}

bool quinceCmpHeight (const quince q1, const quince q2) {
	if (q1.pos > q2.pos)
		return true;
	
	return false;
}

int main() {
	unsigned long gmax = 0;
	unsigned long minPos;
	freopen("gutui.in", "r", stdin);	
	freopen("gutui.out", "w", stdout);

	read();

	sort(v.begin(), v.end(), quinceCmpHeight);
	make_heap(heap.begin(), heap.end(), quinceCmpWeight);

	minPos = v[0].pos;
	print();
	do {
		while(minPos && v.size() && v[0].pos == minPos) {
			heap.insert(heap.end(), v[0]);
			push_heap(heap.begin(), heap.end(), quinceCmpWeight);
			v.erase(v.begin());
		}

		gmax += heap.front().w;
		heap.erase(heap.begin());
		
		make_heap(heap.begin(), heap.end(), quinceCmpWeight);		
		
		if(v.size())
			minPos = v[0].pos;
		else
			minPos--;	
	} while(minPos);
	
	printf("%d\n", gmax);

	return 0;
}