Pagini recente » Cod sursa (job #1116065) | Cod sursa (job #2337030) | Cod sursa (job #1439983) | Cod sursa (job #1517765) | Cod sursa (job #738494)
Cod sursa(job #738494)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int Nr;
vector<int> H;
vector<int> poz;
int father(int x)
{
return x/2;
}
int left(int x)
{
return x*2;
}
int right(int x)
{
return x*2 + 1;
}
void sift(int N, int k)
{
int son;
do{
son = 0;
if(left(k) <= N) {
son = left(k);
if(right(k) <= N && H[left(k)] > H[right(k)]) {
son = right(k);
}
if(H[k] <= H[son]) {
son = 0;
}
}
if(son) {
swap(H[k], H[son]);
k = son;
}
}while(son);
}
void perc(int N, int k)
{
int key = H[k];
while (k > 1 && key < H[father(k)]) {
H[k] = H[father(k)];
k = father(k);
}
H[k] = key;
}
void cut(int& N, int k) {
H[k] = H[N];
--N;
if (k > 1 && H[k] < H[father(k)]) {
perc(N, k);
} else {
sift(N, k);
}
}
void insert(int& N, int elem)
{
H.push_back(elem);
perc(N, N);
}
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int main()
{
H.push_back(0);
poz.push_back(0);
int cod, x;
int i,j;
int L = 0;
f>>Nr;
for(i = 1; i <= Nr; i++) {
f>>cod;
switch(cod) {
case 1:
f>>x;
L++;
insert(L, x);
poz.push_back(x);
break;
case 2:
f>>x;
for(j = 1 ; j <= L; j++) {
if(H[j] == poz[x])
break;
}
cut(L, j);
break;
case 3:
g<<H[1]<<endl;
}
}
}