Pagini recente » Cod sursa (job #486752) | Cod sursa (job #2849175) | Borderou de evaluare (job #2864092) | Cod sursa (job #2811464)
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
vector<int>v(1, 0);
vector<int>ord;
bool empty()
{
return v.size() == 1;
}
void up(int pos)
{
while(pos > 1 && v[pos] < v[pos / 2])
{
swap(v[pos], v[pos / 2]);
pos /= 2;
}
}
void down(int pos)
{
while(2 * pos < v.size())
{
int min_fiu = 2 * pos;
if(2 * pos + 1 < v.size() && v[2 * pos + 1] < v[min_fiu])
{
min_fiu = 2 * pos + 1;
}
if(v[min_fiu] >= v[pos])
{
break;
}
swap(v[pos], v[min_fiu]);
pos = min_fiu;
}
}
int main()
{
int t; cin >> t;
for(int i = 0; i < t; i++)
{
char cerinta; cin >> cerinta;
if(cerinta == '1')
{
int x; cin >> x;
v.push_back(x);
up(v.size() - 1);
ord.push_back(x);
}
else if(cerinta == '2')
{
int poz; cin >> poz;
int ind = 0;
for(int i = 1; i < v.size(); i++)
{
if(ord[poz - 1] == v[i])
{
ind = i;
}
}
swap(v[ind], v[v.size() - 1]);
v.pop_back();
up(ind);
down(ind);
}
else
{
if(!empty())
{
cout << v[1] << "\n";
}
}
}
return false;
}