Pagini recente » Cod sursa (job #1809050) | Cod sursa (job #577205) | Cod sursa (job #2813938) | Cod sursa (job #2351638) | Cod sursa (job #3129329)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
vector<int> ordine;
void push(vector<int> &h, int x)
{
h.push_back(x);
int i = h.size()-1, aux = i;
ordine.push_back(i);
while(h[i] < h[i/2]){
swap(h[i], h[i/2]);
ordine[aux] = i/2;
ordine[i/2] = i;
i/=2;
}
}
void delete_element(vector<int> &h, int n)
{
swap(h[ordine[n]], h[h.size()-1]);
h.pop_back();
int i=ordine[n];
while((h[i] > h[2*i] || h[i] > h[2*i+1]) && 2*i < h.size()){
if(h[i] > h[2*i]){
swap(h[i], h[2*i]);
swap(ordine[i], ordine[2*i]);
i *= 2;
}
else if(h[i] > h[2*i+1]){
swap(h[i], h[2*i+1]);
swap(ordine[i], ordine[2*i+1]);
i = 2*i + 1;
}
}
}
void extract_min(vector<int> &h)
{
g<<h[1]<<endl;
}
int main()
{
ordine.push_back(-1);
vector<int> h;
h.push_back(-1);
int n, x, y;
f>>n;
for(int i=0; i<n; i++){
f>>x;
switch(x)
{
case 1:{
f>>y;
push(h, y);
break;
}
case 2:{
f>>y;
delete_element(h, y);
break;
}
case 3:{
extract_min(h);
break;
}
}
// for(int i=1; i<h.size(); i++)
// cout<<h[i]<<" ";
// cout<<endl;
// for(int i=1; i<ordine.size(); i++)
// cout<<ordine[i]<<" ";
// cout<<endl<<endl;
// }
// push(h, 3);
// push(h, 4);
// push(h, 1);
// push(h, 2);
// push(h, 6);
// push(h, 5);
// extract_min(h);
// delete_element(h, 3);
// extract_min(h);
// delete_element(h, 2);
// for(int i=1; i<h.size(); i++)
// cout<<h[i]<<" ";
// cout<<endl;
// for(int i=1; i<ordine.size(); i++)
// cout<<ordine[i]<<" ";
return 0;
}