Pagini recente » Cod sursa (job #774927) | Cod sursa (job #2475856) | Cod sursa (job #2204518) | Cod sursa (job #2045281) | Cod sursa (job #2894891)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n;
vector<int>V;
vector<int>Poz;
void insert(int x,int nr) {
int i;
V.push_back(x);
Poz.push_back(nr);
i = V.size() - 1;
while (i) {
if (V[i] < V[(i-1)/2]) {
swap(V[i], V[(i-1)/2]);
swap(Poz[i], Poz[(i-1)/2]);
i = (i-1)/2;
}
else {
break;
}
}
cout<<"insert"<<endl;
for(int j = 0; j < V.size(); j++){
cout<<V[j]<<" ";
}
cout<<endl;
for(int j = 0; j < Poz.size(); j++){
cout<<Poz[j]<<" ";
}
cout<<endl<<"insertgata"<<endl;
}
void arrange(int i) {
if (i * 2 + 1 >= V.size())
return;
int st = V[i * 2 + 1];
if ((i * 2 + 2 == V.size()) || st < V[i * 2 + 2]){
if (st < V[i]) {
swap(V[i], V[i * 2 + 1]);
swap(Poz[i], Poz[i * 2 + 1]);
arrange(i * 2 + 1);
return;
}
else {
return;
}
}
else {
if (V[i * 2 + 2] < V[i]) {
swap(V[i], V[i * 2 + 2]);
swap(Poz[i], Poz[i * 2 + 2]);
arrange(i * 2 + 2);
return;
}
else {
return;
}
}
}
void erase(int x) {
int i;
for (i = 0; i < Poz.size(); i++) {
if (Poz[i] == x) {
break;
}
}
V[i] = V[V.size() - 1];
Poz[i] = Poz[Poz.size() - 1];
V.pop_back();
Poz.pop_back();
arrange(i);
cout<<"erase"<<endl;
for(int j = 0; j < V.size(); j++){
cout<<V[j]<<" ";
}
cout<<endl;
for(int j=0; j < Poz.size(); j++){
cout<<Poz[j]<<" ";
}
cout<<endl<<"erasegata"<<endl;
}
int main() {
int cod,x,nr=-1;
f>>n;
while(n!=0)
{
f>>cod;
if(cod==1){
f>>x;
nr++;
insert(x,nr);
}
else if(cod==2){
f>>x;
x = x-1;
erase(x);
}
else if(cod==3){
g<<V[0]<<"\n";
}
n--;
}
return 0;
}