Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <climits>
  4. #define _CRT_DISABLE_PERFCRIT_LOCKS
  5.  
  6. using namespace std;
  7.  
  8. void heap(int arr[], int cur, int end)
  9. {
  10.     int child = 2 * cur + 1;
  11.     while (child <= end)
  12.     {
  13.         if (child < end && arr[child] > arr[child + 1])
  14.             child++;
  15.         if (arr[cur] > arr[child])
  16.         {
  17.             swap(arr[child], arr[cur]);
  18.             cur = child;
  19.             child = 2 * cur + 1;
  20.         }
  21.         else return;
  22.     }
  23.  
  24. }
  25.  
  26. int main()
  27. {
  28.     freopen("input.txt", "r", stdin);
  29.  
  30.     freopen("output.txt", "w", stdout);
  31.     cin.tie(NULL);
  32.     int n, a, k, min;
  33.     ios_base::sync_with_stdio(false);
  34.     cin >> n >> k;
  35.     int arr[100000];
  36.     for (int i = 0; i < k; i++)
  37.     {
  38.         arr[i] = INT_MIN;
  39.     }
  40.     for (int i = 0; i < n; i++)
  41.     {
  42.         ios_base::sync_with_stdio(false);
  43.         cin >> a;
  44.         if (a > arr[0])
  45.         {
  46.             arr[0] = a;
  47.             heap(arr, 0, k - 1);
  48.         }
  49.     }
  50.     ios_base::sync_with_stdio(false);
  51.     cout << arr[0];
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement