Pagini recente » Cod sursa (job #1903436) | Cod sursa (job #433601) | Cod sursa (job #626863) | Cod sursa (job #86785) | Cod sursa (job #2330201)
#define nmax 100
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[nmax],pos[nmax],invPos[nmax];
int heapSize,amm;
void addToHeap(int key){
heapSize++;
amm++;
heap[heapSize] = key;
pos[heapSize] = amm;
invPos[pos[heapSize]] = pos[heapSize];
int node = heapSize;
while(node/2 >= 1 && heap[node] < heap[node/2]){
swap(heap[node],heap[node/2]);
swap(invPos[pos[node]],invPos[pos[node/2]]);
swap(pos[node],pos[node/2]);
node/=2;
}
}
void removeFromHeap(int key){
int node = invPos[key];
swap(heap[node],heap[heapSize]);
swap(invPos[pos[node]],invPos[pos[heapSize]]);
swap(pos[node],pos[heapSize]);
heapSize--;
bool goUp = false;
while(node/2 >= 1 && heap[node] < heap[node/2]){
swap(heap[node],heap[node/2]);
swap(invPos[pos[node]],invPos[pos[node/2]]);
swap(pos[node],pos[node/2]);
node/=2;
goUp = true;
}
if(goUp == false){
while(node*2 <= heapSize){
if(node*2+1 <= heapSize){
if(heap[node*2] < heap[node*2+1] && heap[node] > heap[node*2]){
swap(heap[node*2],heap[node]);
swap(invPos[pos[node*2]],invPos[pos[node]]);
swap(pos[node*2],pos[node]);
node*=2;
continue;
}
else if(heap[node] > heap[node*2+1]){
swap(heap[node*2+1],heap[node]);
swap(invPos[pos[node*2+1]],invPos[pos[node]]);
swap(pos[node*2+1],pos[node]);
node*=2;
node++;
continue;
}
}
else{
if(heap[node] > heap[node*2]){
swap(heap[node*2],heap[node]);
swap(invPos[pos[node*2]],invPos[pos[node]]);
swap(pos[node*2],pos[node]);
node*=2;
continue;
}
}
break;
}
}
}
// 1(3)
// 4(1) 3(2)
// 3 1 2 - pos
// 2 3 1 - invPos
int main(){
int n;
fin>>n;
int i,key,opp;
for(i = 0; i < n; i++){
fin>>opp;
if(opp == 1){
fin>>key;
addToHeap(key);
// fout<<"\n"<<opp<<" "<<key<<"\n";
// fout<<"-----\n";
// for(j = 0; j < heapSize+1; j++){
// fout<<heap[j]<<" ";
// }
// fout<<"\n";
// for(j = 0; j < 60; j++){
// fout<<pos[j]<<" ";
// }
// fout<<"\n";
// for(j = 0; j < 60; j++){
// fout<<invPos[j]<<" ";
// }
}
else if(opp == 2){
fin>>key;
removeFromHeap(key);
// fout<<"\n"<<opp<<" "<<key<<"\n";
// fout<<"-----\n";
// for(j = 0; j < heapSize+1; j++){
// fout<<heap[j]<<" ";
// }
// fout<<"\n";
// for(j = 0; j < 60; j++){
// fout<<pos[j]<<" ";
// }
// fout<<"\n";
// for(j = 0; j < 60; j++){
// fout<<invPos[j]<<" ";
// }
}
else if(opp == 3){
fout<<heap[1]<<"\n";
// fout<<"\n"<<opp<<"\n";
// fout<<"-----\n";
// for(j = 0; j < heapSize+1; j++){
// fout<<heap[j]<<" ";
// }
// fout<<"\n";
// for(j = 0; j < 60; j++){
// fout<<pos[j]<<" ";
// }
// fout<<"\n";
// for(j = 0; j < 60; j++){
// fout<<invPos[j]<<" ";
// }
}
}
}