Advertisement
smatskevich

Seminar4

Oct 10th, 2022
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4.  
  5. int main0() {
  6.   std::ios::sync_with_stdio(false);
  7.   std::cin.tie(nullptr);
  8.   std::cout.tie(nullptr);
  9.  
  10.   int value = 0;
  11.   char num = getchar();
  12.   bool negative = num == '-';
  13.  
  14.   if ('0' <= num && num <= '9')
  15.     value = num - '0';
  16.   while (true) {
  17.     num = getchar();
  18.     if ('0' <= num && num <= '9') {
  19.       value = value * 10 + (num - '0');
  20.     } else {
  21.       break;
  22.     }
  23.   }
  24.   if (negative) value = -value;
  25.   std::cout << value << std::endl;
  26.   return 0;
  27. }
  28.  
  29. int main1() {
  30.   std::priority_queue<int, std::vector<int>, std::greater<>> q;
  31.   q.push(0);  // O(log n)
  32.   q.push(2);
  33.   q.push(5);
  34.   q.push(1);
  35.   std::cout << q.top() << std::endl;  // O(1)
  36.   q.pop();  // O(log n)
  37.   std::cout << q.top() << std::endl;
  38.   return 0;
  39. }
  40.  
  41. void print(const std::vector<int>& v) {
  42.   for (int x : v) std::cout << x << " ";
  43.   std::cout << std::endl;
  44. }
  45.  
  46. int main2() {
  47.   std::vector<int> v = {6, 5, 7, 3, 2, 1, 4, 8, 9, 0};
  48.   print(v);
  49.  
  50.   std::make_heap(v.begin() + 1, v.end() - 3);
  51.   print(v);
  52.  
  53.   *(v.end() - 4) = 10;
  54.   std::push_heap(v.begin() + 1, v.end() - 3);
  55.   print(v);
  56.  
  57.   std::pop_heap(v.begin() + 1, v.end() - 3);
  58.   print(v);
  59.  
  60.   return 0;
  61. }
  62.  
  63. class PriorityQueue {
  64.  public:
  65.   void Push(int value);
  66.   int Pop();
  67.   bool Empty() const { return v.empty(); }
  68.  
  69.  private:
  70.   std::vector<int> v;
  71.  
  72.   void SiftUp(size_t index);
  73.   void SiftDown(size_t index);
  74.  
  75. };
  76.  
  77. void PriorityQueue::Push(int value) {
  78.   v.push_back(value);
  79.   SiftUp(v.size() - 1);
  80. }
  81.  
  82. int PriorityQueue::Pop() {
  83.   if (v.empty())
  84.     throw std::runtime_error("Pop() called for empty PriorityQueue");
  85.   int result = v.front();
  86.   v.front() = v.back();
  87.   v.pop_back();
  88.   if (!Empty()) SiftDown(0);
  89.   return result;
  90. }
  91.  
  92. void PriorityQueue::SiftUp(size_t index) {
  93.   while (index > 0) {
  94.     size_t parent = (index - 1) / 2;
  95.     if (v[parent] >= v[index]) return;
  96.     std::swap(v[parent], v[index]);
  97.     index = parent;
  98.   }
  99. }
  100.  
  101. void PriorityQueue::SiftDown(size_t index) {
  102. //  while (true) {
  103. //  while (1) {
  104. //  for (;;) {
  105.   while (true) {
  106.     size_t left = 2 * index + 1;
  107.     size_t right = 2 * index + 2;
  108.     // Ищем большего сына, если такой есть.
  109.     size_t largest = index;
  110.     if (left < v.size() && v[left] > v[index])
  111.       largest = left;
  112.     if (right < v.size() && v[right] > v[largest])
  113.       largest = right;
  114.     // Если больший сын есть, то проталкиваем корень в него.
  115.     if (index == largest) break;
  116.     std::swap(v[index], v[largest]);
  117.     index = largest;
  118.   }
  119. }
  120.  
  121. int main() {
  122.   try {
  123.     PriorityQueue q;
  124.     q.Push(40);
  125.     q.Push(32);
  126.     q.Push(12);
  127.     q.Push(57);
  128.     q.Push(56);
  129.     std::cout << q.Pop() << std::endl;
  130.     std::cout << q.Pop() << std::endl;
  131.     std::cout << q.Pop() << std::endl;
  132.     std::cout << q.Pop() << std::endl;
  133.     std::cout << q.Pop() << std::endl;
  134.     std::cout << q.Pop() << std::endl;
  135.   } catch(std::exception& e) {
  136.     std::cout << e.what();
  137.   }
  138.   return 89;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement