Advertisement
RMarK0

Untitled

Apr 25th, 2021
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. vector<int> heap;
  9. vector<int> heapmax;
  10. void siftUp(int v) {
  11.     if (v == 0) return;
  12.     int p = (v - 1) / 2; //родитель
  13.     if (heap[v] < heap[p]) {
  14.         swap(heap[v], heap[p]);
  15.         siftUp(p);
  16.     }
  17. }
  18. void siftDown(int v) {
  19.     int left = 2 * v + 1;
  20.     int right = 2 * v + 2;
  21.     if (left >= heap.size()) return;
  22.     if (left == heap.size() - 1) right = left;
  23.     int imax = heap[left] < heap[right] ? left : right;
  24.     if (heap[v] > heap[imax]) {
  25.         swap(heap[v], heap[imax]);
  26.         siftDown(imax);
  27.     }
  28.  
  29. }
  30. void siftUpMax(int v) {
  31.     if (v == 0) return;
  32.     int p = (v - 1) / 2; //родитель
  33.     if (heapmax[v] > heapmax[p])
  34.     {
  35.         swap(heapmax[v], heapmax[p]);
  36.         siftUpMax(p);
  37.     }
  38. }
  39. void siftDownMax(int v)
  40. {
  41.     int left = 2 * v + 1;
  42.     int right = 2 * v + 2;
  43.     if (left >= heapmax.size()) return;
  44.     if (left == heapmax.size() - 1) right = left;
  45.     int imax = heapmax[left] > heapmax[right] ? left : right;
  46.     if (heapmax[v] < heapmax[imax])
  47.     {
  48.         swap(heapmax[v], heapmax[imax]);
  49.         siftDownMax(imax);
  50.     }
  51. }
  52.  
  53.  
  54. int main() {
  55.     setlocale(LC_CTYPE, "rus");
  56.     string str;
  57.     cout << "ADD <number> - добавляет новый элемент в heap и heapmax\nMIN - выводит минимальный элемент из heap" << endl
  58.         << "MAX - выводит максимальный элемент из heapmax\nCLEAR - очищает heap и heapmax" << endl;
  59.     while (cin >> str) {
  60.         if (str == "ADD") {
  61.             int x = 0;
  62.             cin >> x;
  63.             heap.push_back(x);
  64.             siftUp(heap.size() - 1);
  65.             heapmax.push_back(x);
  66.             siftUpMax(heapmax.size() - 1);
  67.         }
  68.         else if (str == "MIN") {
  69.             if (!heap.empty()) {
  70.                 cout << heap[0] << '\n';
  71.                 heap[0] = heap.back();
  72.                 heap.pop_back();
  73.                 siftDown(0);
  74.             }
  75.             else {
  76.                 cout << "CANNOT\n";
  77.             }
  78.         }
  79.         else if (str == "MAX")
  80.         {
  81.             if (!heap.empty())
  82.             {
  83.                 cout << heapmax[0] << '\n';
  84.                 heapmax[0] = heapmax.back();
  85.                 heapmax.pop_back();
  86.                 siftDownMax(0);
  87.             }
  88.             else
  89.             {
  90.                 cout << "CANNOT\n";
  91.             }
  92.         }
  93.         else if (str == "CLEAR") {
  94.             heap = vector<int>();
  95.             heapmax = vector<int>();
  96.         }
  97.     }
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement