Pagini recente » Cod sursa (job #837713) | Cod sursa (job #372250) | Cod sursa (job #264160) | Cod sursa (job #67426) | Cod sursa (job #3330746)
// https://infoarena.ro/problema/heapuri - Szücs Patrik - Kevin
#include <fstream>
#include <vector>
#include <utility>
using namespace std;
#define vint vector<int>
#define vpair vector<pair<int, int>>
//--------------------------------------------------------------------
void up(vpair &heap, int indx, vint &order)
{
if (indx)
{
int dad = (indx - 1) / 2;
if (heap[dad].first > heap[indx].first)
{
swap(heap[dad], heap[indx]);
swap(order[heap[dad].second], order[heap[indx].second]);
up(heap, dad, order);
}
}
}
//--------------------------------------------------------------------
void down(vpair &heap, int indx, vint &order)
{
int n = (int)heap.size();
while (true)
{
int l = 2 * indx + 1, r = 2 * indx + 2, min = indx;
if (l < n && heap[l].first < heap[min].first)
min = l;
if (r < n && heap[r].first < heap[min].first)
min = r;
if (min == indx)
break;
swap(order[heap[indx].second], order[heap[min].second]);
swap(heap[indx], heap[min]);
indx = min;
}
/*
int child1 = 2 * indx + 1, child2 = 2 * indx + 2;
if (child1 < (int)heap.size() && heap[child1].first < heap[indx].first)
{
swap(heap[child1], heap[indx]);
swap(order[heap[child1].second], order[heap[indx].second]);
down(heap, child1, order);
}
if (child2 < (int)heap.size() && heap[child2].first < heap[indx].first)
{
swap(heap[child2], heap[indx]);
swap(order[heap[child2].second], order[heap[indx].second]);
down(heap, child2, order);
}
*/
}
//--------------------------------------------------------------------
void push(vpair &heap, int x, vint &order)
{
heap.push_back({x, order.size()}); // igy a legnagyobb elemnek lesz beallitva ezert meg kell nezni felfele, hogy van e tole nagyobb
order.push_back(heap.size() - 1); // berakjuk az orderbe, hogy melyik helyen van az adott elem
up(heap, (int)heap.size() - 1, order); // megadja a heapet es az indxet amin az utolso elem van
}
//--------------------------------------------------------------------
void pop(vpair &heap, int cron, vint &order)
{
/*
int pos = order[cron];
if (pos != (int)heap.size() - 1)
{
// Only swap if not the last element
heap[pos] = heap.back();
order[heap.back().second] = pos;
heap.pop_back();
down(heap, pos, order);
up(heap, pos, order);
}
else
{
// Just remove the last element
heap.pop_back();
}
*/
heap[order[cron]] = heap.back(); // kicserejuk a torlendo elemet az utolso val, majd toroljuk az utolso elemet
order[heap.back().second] = order[cron];
heap.pop_back();
// order.pop_back();
// order[heap[order[cron]].second] = order[cron]; // Az order-ben is frissítjük az indexet
down(heap, order[cron], order);
up(heap, order[cron], order);
}
//--------------------------------------------------------------------
int main()
{
int n;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
in >> n;
vpair heap;
heap.reserve(n); // mivel, ha mind beszuras akkor ennyi elem lehet max
vint order; // Min-Heap
//------------------------
for (int i = 0; i < n; i++)
{
short t; // type
in >> t;
if (t == 1) //------------------------
{
int x;
in >> x;
push(heap, x, order);
}
else if (t == 2) //------------------------
{
int cron;
in >> cron;
pop(heap, cron - 1, order); // cron - 1, mert 0-tol indexelek
}
else //------------------------
{
out << heap[0].first << '\n';
}
}
//------------------------
/*
for (auto elem : heap)
{
out << elem.first << ' ';
}
*/
in.close();
out.close();
return 0;
}