Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5. class S {
  6. public:
  7.     int key;
  8.     int index;
  9.  
  10.        
  11. };
  12. void  SiftDown(S a, vector<S>&heap,int size)
  13. {
  14.     int p = 0, t = 0;
  15.     heap[0].key = a.key;
  16.     heap[0].index = a.index;
  17.     while ((t == 0) && (2 * p + 1 < size))
  18.     {
  19.         if (2 * p + 2 < size)
  20.         {
  21.             if (heap[2 * p + 1].key > heap[2 * p + 2].key)
  22.             {
  23.                 if (heap[p].key > heap[2 * p + 2].key)
  24.                 {
  25.                     swap(heap[p], heap[2 * p + 2]);
  26.                     p = 2 * p + 2;
  27.                 }
  28.                 else
  29.                     t = 1;
  30.             }
  31.             else
  32.             {
  33.                 if (heap[p].key > heap[2 * p + 1].key)
  34.                 {
  35.                     swap(heap[p], heap[2 * p + 1]);
  36.                     p = 2 * p + 1;
  37.                 }
  38.                 else
  39.                     t = 1;
  40.             }
  41.  
  42.         }
  43.         else
  44.  
  45.             if (heap[p].key > heap[2 * p + 1].key)
  46.             {
  47.                 swap(heap[p], heap[2 * p + 1]);
  48.                 p = 2 * p + 1;
  49.             }
  50.             else
  51.                 t = 1;
  52.     }
  53.    
  54.  
  55. }
  56. void buildheap(vector<S>&heap, S a,int &size)
  57. {
  58.     int p = size;
  59.     heap[size].key= a.key;
  60.     heap[size].index = a.index;
  61.     int t = 0;
  62.     while ((t == 0)&&(p>0))
  63.     {
  64.         if (heap[(p - 1) / 2].key > a.key)
  65.         {
  66.             swap(heap[p], heap[(p - 1) / 2]);
  67.             p = (p - 1) / 2;
  68.         }
  69.         else
  70.             t = 1;
  71.  
  72.     }
  73.     ++size;
  74. }
  75. void Sort(vector<S>&heap, vector<S>a,int &size,int k)
  76. {
  77.     int t = size;
  78.     int l = k;
  79.     while (t > 0)
  80.     {
  81.         cout << heap[0].key << " ";
  82.         if (heap[0].index + k < size)
  83.         {
  84.             SiftDown(a[heap[0].index + k], heap, l);
  85.         }
  86.         else
  87.         {
  88.             SiftDown(heap[l - 1], heap, l-1);
  89.             --l;
  90.  
  91.         }
  92.         t--;
  93.    
  94.     }
  95.  
  96. }
  97. int main()
  98. {
  99.     int n, k;
  100.     cin >> n >> k;
  101.     vector<S>a(n);
  102.     vector<S>heap(k);
  103.     int size = 0, b;
  104.     for (int i = 0; i < n; ++i)
  105.     {
  106.         cin >> b;
  107.         a[i].key = b;
  108.         a[i].index = i;
  109.         if (i < k)
  110.         {
  111.             buildheap(heap, a[i], size);
  112.         }
  113.  
  114.     }
  115.     Sort(heap, a, n, k);
  116.     system("pause");
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement