Cod sursa(job #1461430)

Utilizator ramhackNastase Ramon ramhack Data 15 iulie 2015 17:40:15
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.73 kb
#include <iostream>
#include <cstdio>
#include <set>
#include <fstream>

using namespace std;

class HeapSet {
private:
	multiset<int> heapSet;
	static const int MAX = 1000000;
	int poz;
public:
	int *memoryArray;

	HeapSet(): poz(1) { memoryArray = new int[MAX + 1];}
	~HeapSet() { delete[] memoryArray;}


	void insert(const int& value){
		heapSet.insert(value);
		memoryArray[poz] = value;
		poz++;
	}

	void remove(const int& node) {
		heapSet.erase(node);
	}

	bool search(const int& node) {

		if(heapSet.find(node) != heapSet.end()) {
			return true;
		}
		return false;
	}

	int getMin() {

		multiset<int>::const_iterator it = heapSet.begin();

		return *it;
	}

	int getMax() {
	
		multiset<int>::const_iterator it = heapSet.end();
		--it;
		return *it;		
	}

	void printHeapSet() {

		cout << "Heap --> ";
		for(multiset<int>::const_iterator it = heapSet.begin(); it != heapSet.end(); ++it) {

			cout << *it << ' ';
		}
		cout <<endl;
	}

};


int main(int argc, char const *argv[])
{
	FILE *f, *fwr;

	fwr = fopen("heapuri.out", "w");

	//if((f = fopen("grader_test1.in", "r")) == NULL) {
	if((f = fopen("heapuri.in", "r")) == NULL) {
		fprintf(stderr, "Can't open file\n");
		return 0;
	}

	int N, operatie, nr;
	HeapSet *heap = new HeapSet();

	fscanf(f, "%d", &N);

	for(int i = 0; i < N; i++) {

		fscanf(f, "%d", &operatie);

		if(operatie == 1) {
			fscanf(f, "%d", &nr);
			heap->insert(nr);
			//cout << "Add: " << nr << endl;
		}
		else if(operatie == 2) {
			
			fscanf(f, "%d", &nr);
			heap->remove(heap->memoryArray[nr]);
		//	cout << "Delete: " << heap->memoryArray[nr] << endl;
		}
		else if(operatie == 3) {

			fprintf(fwr, "%d\n", heap->getMin());
		//	cout << "Min: " << heap->getMin() << endl;
		}

	}
	
	fclose(f);
	fclose(fwr);

	return 0;
}