Nu aveti permisiuni pentru a descarca fisierul grader_test45.in
Cod sursa(job #1829945)
Utilizator | Data | 15 decembrie 2016 21:12:07 | |
---|---|---|---|
Problema | Heapuri | Scor | 40 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 5.64 kb |
#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(0);
#define setnow clock_t tStart=clock();
#define time (double)(clock() - tStart)/CLOCKS_PER_SEC;
#define endl '\n'
typedef long long ll;
typedef long long int lli;
typedef pair < int, int> dbl;
const int maxInt = 1e9*2;
const lli maxLong = 1e18*2;
int n;
int heap[101010];
int length;
int nbr = 0;
int op,x;
int mp[101010];
int mpx[101010];
//map <int, int> mpx;
void insert(int x){
if(length == 0){
heap[length++] = x;
nbr++;
mp[length - 1] = nbr;
mpx[nbr] = length - 1;
return;
}
heap[length++] = x;
nbr++;
mp[length - 1] = nbr;
mpx[nbr] = length - 1;
int pos = length - 1;
bool minHeap = false;
while(pos > 0 && !minHeap){
if(heap[pos] < heap[(pos - 1)/ 2 ]){
swap(mp[pos], mp[(pos - 1) / 2]);
swap(heap[pos], heap[(pos - 1) / 2]);
int one = mp[(pos - 1) / 2];
int two = mp[pos];
swap(mpx[one],mpx[two]);
pos = (pos - 1) / 2;
} else
minHeap = true;
}
}
void erase(int x){
swap(heap[length - 1],heap[x]);
swap(mp[length - 1],mp[x]);
int one = mp[length - 1];
int two = mp[x];
swap(mpx[one],mpx[two]);
//cout << length << endl;
//
//
length--;
int pos = x;
//
// cout << "hhpp:" << endl;
// for(int i = 0; i < length; i++)
// cout << heap[i] << ' ';
// cout << endl;
//
if(pos > 0 && heap[pos] < heap[(pos - 1) / 2]){
bool minHeap = false;
while(pos > 0 && !minHeap){
if(heap[pos] < heap[(pos - 1)/ 2 ]){
swap(mp[pos], mp[(pos - 1) / 2]);
swap(heap[pos], heap[(pos - 1) / 2]);
int one = mp[(pos - 1) / 2];
int two = mp[pos];
swap(mpx[one],mpx[two]);
pos = (pos - 1) / 2;
} else
minHeap = true;
}
} else
if((heap[pos] > heap[pos * 2 + 1] && pos * 2 + 1 < length) || (heap[pos] > heap[pos * 2 + 2] && pos * 2 + 2 < length)){
bool minHeap = false;
while(2 * pos + 1 < length && !minHeap){
if(pos * 2 + 2 < length && heap[pos] > heap[pos * 2 + 1] && heap[pos] > heap[pos * 2 + 2]){
//cout << "HEREEE" << endl;
if(heap[pos * 2 + 1] < heap[pos * 2 + 2]){
swap(heap[pos], heap[pos * 2 + 1]);
swap(mp[pos], mp[pos * 2 + 1]);
int one = mp[pos * 2 + 1];
int two = mp[pos];
swap(mpx[one],mpx[two]);
pos = pos * 2 + 1;
} else {
swap(mp[pos], mp[pos * 2 + 2]);
swap(heap[pos], heap[pos * 2 + 2]);
int one = mp[pos * 2 + 2];
int two = mp[pos];
swap(mpx[one],mpx[two]);
pos = pos * 2 + 2;
}
} else
if(heap[pos] > heap[pos * 2 + 1]){
swap(mp[pos], mp[pos * 2 + 1]);
swap(heap[pos], heap[pos * 2 + 1]);
int one = mp[pos * 2 + 1];
int two = mp[pos];
swap(mpx[one],mpx[two]);
pos = pos * 2 + 1;
} else
if(pos * 2 + 2 < length && heap[pos] > heap[pos * 2 + 2]){
swap(mp[pos], mp[pos * 2 + 2]);
swap(heap[pos], heap[pos * 2 + 2]);
int one = mp[pos * 2 + 1];
int two = mp[pos];
swap(mpx[one],mpx[two]);
pos = pos * 2 + 2;
} else minHeap = true;
}
}
//
//
}
int main(){
ios;
// setnow;
// ifstream cin("input.in");
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
cin >> n;
for(int i = 0; i < n; i++){
cin >> op;
if(op == 1){
cin >> x;
insert(x);
}
else
if(op == 2){
cin >> x;
// int ps;
// for( ps = 0; ps < nbr; ps++)
// if(x == mp[ps])
// break;
//cout << "HH: " << endl;
// cout << "x : " << x << ' ' << " ps: " << ps << ' ' << endl;
// cout << mpx[x] << endl;
//cout << "----------------->" << endl;
erase(mpx[x]);
} else
if(op == 3)
cout <<heap[0] << endl;
}
return 0;
}