Pagini recente » Borderou de evaluare (job #2791612) | Cod sursa (job #597722) | Cod sursa (job #2296534)
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_SIZE = 200005;
typedef int Heap[MAX_SIZE];
int order[MAX_SIZE];
inline int father(int nod){
return nod / 2;
}
inline int leftSon(int nod){
return nod * 2;
}
inline int rightSon(int nod){
return nod * 2 + 1;
}
inline int max(Heap H){
return H[1];
}
void sift(Heap H,int n,int k){
int son;
do{
son = 0;
if(leftSon(k) <= n){
son = leftSon(k);
if(rightSon(k) <= n && H[rightSon(k)] < H[leftSon(k)])
son = rightSon(k);
if(H[son] >= H[k])
son = 0;
}
if(son){
swap(H[k],H[son]);
k = son;
}
}while(son);
}
void percolate(Heap H,int n,int k){
int key = H[k];
while(k > 1 && H[father(k)] > key){
H[k] = H[father(k)];
k = father(k);
}
H[k] = key;
}
void buildHeap(Heap H,int n){
for(int i=n/2;i>=1;--i)
sift(H,n,i);
}
void cut(Heap H,int &n,int k){
H[k] = H[n];
n--;
if(k > 1 && H[k] < H[father(k)])
percolate(H,n,k);
else
sift(H,n,k);
}
void insert(Heap H,int &n, int value){
H[++n] = value;
percolate(H,n,n);
}
void sortHeap(Heap H,int n){
buildHeap(H,n);
for(int i=n;i>=2;i--){
H[1] = H[i];
sift(H,i-1,i);
}
}
void write(Heap H,int n){
for(int i=1;i<=n;i++)
cout << H[i] << " ";
cout << "\n";
}
void read(Heap H){
fstream in("heapuri.in",ios::in);
fstream out("heapuri.out",ios::out);
int number_op,n = 0,ord =0;
in >> number_op;
for(int i=1;i<=number_op;++i){
int code,value;
in >> code;
if(code == 3)
out << max(H) << "\n";
if(code == 1){
in >> value;
order[++ord] = value;
insert(H,n,value);
}
if(code == 2){
int position;
in >> value;
for(int j=1;j<=n;j++)
if(H[j] == order[value])
position = j;
cut(H,n,position);
}
}
in.close();
out.close();
}
int main()
{
Heap H;
read(H);
return 0;
}