yordanganev

Untitled

Nov 29th, 2020
332
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <inttypes.h>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int32_t  find_max_idx(int * numbers, int32_t root)
  8. {
  9.     while ( numbers[root*2+2] != 0)
  10.     {
  11.         root = root*2+2;
  12.     }
  13.     return root;
  14. }
  15.  
  16. int main()
  17. {
  18.     // 5 1 4 2 7 1 3
  19.     // 6 5 10 8 12 7 9 11
  20.     // 8 1 10 8 12 7 9 11
  21.     // 7 5 54 33 59 13 45 56 64
  22.  
  23.     int32_t numbers[100000] = { 0 };
  24.     int32_t sorted[100000] = {0};
  25.  
  26.     // get input
  27.     int32_t n, p;
  28.     cin >> n >> p;
  29.  
  30.     int32_t inputIdx = 0;
  31.     if ( n )
  32.         cin >> numbers[inputIdx++];
  33.  
  34.     while (inputIdx < n )
  35.     {
  36.         cin >>  numbers[inputIdx];
  37.         sorted[inputIdx] = numbers[inputIdx];
  38.         inputIdx++;
  39.     }
  40.  
  41.  
  42.     // find P-th highest
  43.     sort(sorted, sorted + n - 1);
  44.     int32_t search = sorted[n-p-1];
  45.     cout << search << endl;
  46.  
  47.     int32_t lastMaxIdx = find_max_idx(numbers, 0); // start from right side
  48.     cout << "Max idx: " << lastMaxIdx <<":"<< numbers[lastMaxIdx] << endl;
  49.  
  50.     int32_t pCountDown = p-1;
  51.  
  52.     while (pCountDown--)
  53.     {
  54.  
  55.     }
  56.  
  57.  
  58.     // remove P-th highest
  59.     int32_t rmIndex = lastMaxIdx;
  60.     int32_t minChildIndex = rmIndex*2+2;
  61.  
  62.     cout << "rmIndex: " << rmIndex <<":"<< numbers[rmIndex] << endl;
  63.     if ( numbers[rmIndex*2+2] != 0 )
  64.     {
  65.         while (numbers[minChildIndex*2+1] != 0)
  66.         {
  67.             minChildIndex = minChildIndex*2+1;
  68.         }
  69.     }
  70.     else
  71.     {
  72.         numbers[rmIndex] = 0;
  73.     }
  74.  
  75.     cout << minChildIndex << " " << numbers[minChildIndex] << endl;
  76.  
  77.     numbers[rmIndex] = numbers[minChildIndex];
  78.     numbers[minChildIndex] = 0;
  79.  
  80.     // Display
  81.     inputIdx = 0;
  82.     while (inputIdx < n)
  83.     {
  84.         if ( numbers[inputIdx] != 0)
  85.         {
  86.             cout << numbers[inputIdx] << " ";
  87.         }
  88.         inputIdx++;
  89.     }
  90.     cout << endl;
  91. }
  92.  
RAW Paste Data