Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <inttypes.h>
- #include <algorithm>
- using namespace std;
- int32_t find_max_idx(int * numbers, int32_t root)
- {
- while ( numbers[root*2+2] != 0)
- {
- root = root*2+2;
- }
- return root;
- }
- int main()
- {
- // 5 1 4 2 7 1 3
- // 6 5 10 8 12 7 9 11
- // 8 1 10 8 12 7 9 11
- // 7 5 54 33 59 13 45 56 64
- int32_t numbers[100000] = { 0 };
- int32_t sorted[100000] = {0};
- // get input
- int32_t n, p;
- cin >> n >> p;
- int32_t inputIdx = 0;
- if ( n )
- cin >> numbers[inputIdx++];
- while (inputIdx < n )
- {
- cin >> numbers[inputIdx];
- sorted[inputIdx] = numbers[inputIdx];
- inputIdx++;
- }
- // find P-th highest
- sort(sorted, sorted + n - 1);
- int32_t search = sorted[n-p-1];
- cout << search << endl;
- int32_t lastMaxIdx = find_max_idx(numbers, 0); // start from right side
- cout << "Max idx: " << lastMaxIdx <<":"<< numbers[lastMaxIdx] << endl;
- int32_t pCountDown = p-1;
- while (pCountDown--)
- {
- }
- // remove P-th highest
- int32_t rmIndex = lastMaxIdx;
- int32_t minChildIndex = rmIndex*2+2;
- cout << "rmIndex: " << rmIndex <<":"<< numbers[rmIndex] << endl;
- if ( numbers[rmIndex*2+2] != 0 )
- {
- while (numbers[minChildIndex*2+1] != 0)
- {
- minChildIndex = minChildIndex*2+1;
- }
- }
- else
- {
- numbers[rmIndex] = 0;
- }
- cout << minChildIndex << " " << numbers[minChildIndex] << endl;
- numbers[rmIndex] = numbers[minChildIndex];
- numbers[minChildIndex] = 0;
- // Display
- inputIdx = 0;
- while (inputIdx < n)
- {
- if ( numbers[inputIdx] != 0)
- {
- cout << numbers[inputIdx] << " ";
- }
- inputIdx++;
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement