Cod sursa(job #1654444)

Utilizator andreioneaAndrei Onea andreionea Data 17 martie 2016 01:10:30
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
#include <vector>
#include <algorithm>
int main()
{
	std::ifstream f("heapuri.in");
	std::ofstream g("heapuri.out");
	std::vector<uint32_t> heap;
	std::vector<uint32_t> vec; 
	uint32_t N;
	uint32_t code, val;
	f >> N;
	heap.reserve(N);
	vec.reserve(N);
	while (N--)
	{
		f >> code;
		if (code == 3)
		{
			std::make_heap(heap.begin(), heap.end(), std::greater<uint32_t>());
			g << heap.front() << "\n";
		}
		else
		{
			f >> val;
			if (code == 1)
			{
				heap.push_back(val);
				vec.push_back(val);
			}
			else
			{
				const auto valToFind = vec[val - 1];
				auto it = std::find(heap.begin(), heap.end(), valToFind);
				*it = heap.back();
				heap.pop_back();
			}
		}
	}
	f.close();
	g.close();
	return 0;
}