Max_Leb

Золотая середина

Jun 15th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. # Золотая середина
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6.  
  7. void swap(int *a, int *b) {
  8.     int tmp = *a;
  9.     *a = *b;
  10.     *b = tmp;
  11. }
  12.  
  13.  
  14. void siftDownMax(int *heap, int i, int size) {
  15.     int left, right, j;
  16.     while (2 * i + 1 < size) {
  17.         left = 2 * i + 1;
  18.         right = 2 * i + 2;
  19.         j = left;
  20.         if (right < size and heap[right] > heap[left])
  21.             j = right;
  22.         if (heap[i] >= heap[j])
  23.             break;
  24.         swap(&heap[i], &heap[j]);
  25.         i = j;
  26.     }
  27. }
  28.  
  29.  
  30. void siftDownMin(int *heap, int i, int size) {
  31.     int left, right, j;
  32.     while (2 * i + 1 < size) {
  33.         left = 2 * i + 1;
  34.         right = 2 * i + 2;
  35.         j = left;
  36.         if (right < size and heap[right] < heap[left])
  37.             j = right;
  38.         if (heap[i] <= heap[j])
  39.             break;
  40.         swap(&heap[i], &heap[j]);
  41.         i = j;
  42.     }
  43. }
  44.  
  45.  
  46. void siftUpMax(int *heap, int i) {
  47.     while (heap[i] > heap[(i - 1) / 2]) {
  48.         swap(&heap[i], &heap[(i - 1) / 2]);
  49.         i = (i - 1) / 2;
  50.     }
  51. }
  52.  
  53.  
  54. void siftUpMin(int *heap, int i) {
  55.     while (heap[i] < heap[(i - 1) / 2]) {
  56.         swap(&heap[i], &heap[(i - 1) / 2]);
  57.         i = (i - 1) / 2;
  58.     }
  59. }
  60.  
  61.  
  62. int getPopMin(int *heap, int *size) {
  63.     int min = heap[0];
  64.     heap[0] = heap[*size - 1];
  65.     *size = *size - 1;
  66.     siftDownMin(heap, 0, *size);
  67.     return min;
  68. }
  69.  
  70.  
  71. int getPopMax(int *heap, int *size) {
  72.     int max = heap[0];
  73.     heap[0] = heap[*size - 1];
  74.     *size = *size - 1;
  75.     siftDownMax(heap, 0, *size);
  76.     return max;
  77. }
  78.  
  79.  
  80. void addToMax(int *heap, int key, int *size) {
  81.     *size = *size + 1;
  82.     heap[*size - 1] = key;
  83.     siftUpMax(heap, *size - 1);
  84. }
  85.  
  86.  
  87. void addToMin(int *heap, int key, int *size) {
  88.     *size = *size + 1;
  89.     heap[*size - 1] = key;
  90.     siftUpMin(heap, *size - 1);
  91. }
  92.  
  93.  
  94. int main() {
  95.     std::ifstream fin("input.txt");
  96.     std::ofstream fout("output.txt");
  97.  
  98.     int n;
  99.     fin >> n;
  100.  
  101.     int heapMax[n / 2 + 2], sizeMax = 0;
  102.     int heapMin[n / 2 + 2], sizeMin = 0;
  103.  
  104.     int input;
  105.     fin >> input;
  106.     addToMax(heapMax, input, &sizeMax);
  107.     fout << heapMax[0] << " ";
  108.  
  109.     for (int i = 0; i < n - 1; i++) {
  110.         fin >> input;
  111.         if (input > heapMax[0]) {
  112.             addToMin(heapMin, input, &sizeMin);
  113.         }
  114.         else {
  115.             addToMax(heapMax, input, &sizeMax);
  116.         }
  117.  
  118.         if (sizeMin > sizeMax) {
  119.             int root = getPopMin(heapMin, &sizeMin);
  120.             addToMax(heapMax, root, &sizeMax);
  121.         }
  122.         else if (sizeMax - 1 > sizeMin) {
  123.             int root = getPopMax(heapMax, &sizeMax);
  124.             addToMin(heapMin, root, &sizeMin);
  125.         }
  126.         fout << heapMax[0] << " ";
  127.     }
  128.  
  129.     fin.close();
  130.     fout.close();
  131.  
  132.  
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment