Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- long long Heap[1000000], size = 0;
- void siftUp (int v) {
- while (v > 0 && Heap[v] < Heap[(v - 1) / 2]) {
- swap(Heap[v], Heap[(v - 1) / 2]);
- v = (v - 1) / 2;
- }
- }
- void siftDown (int v) {
- while (2 * v + 1 < size) {
- int left = 2 * v + 1;
- int right = 2 * v + 2;
- int minCh = left;
- if (right < size && Heap[right] < Heap[left])
- minCh = right;
- if (Heap[v] <= Heap[minCh])
- return;
- swap(Heap[v], Heap[minCh]);
- v = minCh;
- }
- }
- void insert (int x) {
- Heap[size++] = x;
- siftUp(size - 1);
- }
- int extractMax() {
- int t = Heap[0];
- Heap[0] = Heap[--size];
- siftDown(0);
- return t;
- }
- int main(){
- int quant, cmd, N;
- cin>>quant;
- for(int i = 0; i < quant; i++){
- cin>>cmd;
- if(cmd == 1)
- size = 0;
- if(cmd == 2){
- cin>>N;
- insert(N);
- }
- if((cmd==3)&&(size != 0))
- cout << extractMax() << endl;
- else if(cmd == 3){
- cout<<"CANNOT"<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement