Nu aveti permisiuni pentru a descarca fisierul grader_test3.ok
Cod sursa(job #1930386)
Utilizator | Data | 18 martie 2017 20:22:56 | |
---|---|---|---|
Problema | Heapuri | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.82 kb |
#include <fstream>
#define nmax 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct heap
{
int nod,weight;
};
class Heap
{
public:
heap a[nmax];
int poz[nmax],sze,lbls;
void Delete(int x)
{
int i = poz[x];
swap(a[sze],a[i]);
sze--;
heapify(i);
while(i > 1 && a[i].weight < a[i / 2].weight)
{
swap(a[i],a[i / 2]);
poz[a[i].nod] = i;
poz[a[i / 2].nod] = i / 2;
i = i / 2;
}
}
void Insert(int w)
{
++lbls;
++sze;
a[sze].nod = lbls;
a[sze].weight = w;
int i = sze;
while(i > 1 && a[i].weight < a[i / 2].weight)
{
swap(a[i],a[i / 2]);
poz[a[i].nod] = i;
poz[a[i / 2].nod] = i / 2;
i = i / 2;
}
}
void heapify(int i)
{
int smallest = i;
if(2 * i <= sze && a[2 * i].weight < a[smallest].weight)
smallest = 2 * i;
if(2 * i + 1 <= sze && a[2 * i + 1].weight < a[smallest].weight)
smallest = 2 * i + 1;
swap(a[smallest],a[i]);
poz[a[smallest].nod] = smallest;
poz[a[i].nod] = i;
if(smallest != i)
heapify(smallest);
}
heap Minimum()
{
return a[1];
}
};
Heap hp;
int n;
int main()
{
int t,x;
fin >> n;
for(int i = 1;i <= n;i++)
{
fin >> t;
if(t == 1)
{
fin >> x;
hp.Insert(x);
}
if(t == 2)
{
fin >> x;
hp.Delete(x);
}
if(t == 3)
{
fout << hp.Minimum().weight << "\n";
}
}
return 0;
}