Advertisement
allia

к наименьший

Oct 26th, 2020
2,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdio.h>
  4. using namespace std;
  5.  
  6. void sift (int *arr, int first, int last)
  7.  
  8.  {
  9.   int j = first;
  10.   int k = first*2+1;
  11.  
  12.    while (k <= last)
  13.   {
  14.     if (k < last && arr[k] < arr[k+1])
  15.      k++; // больший сын
  16.      if (arr[j] < arr[k])
  17.      {
  18.         swap(arr[j], arr[k]);
  19.          j = k;
  20.          k = k*2+1;
  21.      }
  22.    else break;
  23.  }
  24. }
  25.  
  26. void sort (int *arr, int razmer)
  27. {
  28.   for (int i = razmer/2-1; i >= 0; i--)
  29.    sift (arr, i, razmer-1);
  30. }
  31.  
  32. int main()
  33. {
  34.   int n = 0, k = 0, znach = 0, x = 1, shetchik = 0;
  35.  
  36.   scanf("%d", &n);
  37.   scanf("%d", &k);
  38.  
  39.   int *result = new int[k];
  40.  
  41. for (int i = 0; i < k; i++)
  42.   {
  43.     scanf("%d", &znach);
  44.     result[i] = znach;
  45.   }
  46.  
  47. sort (result, k);
  48.  
  49. for (int i = k; i < n; i++)
  50.   {
  51.    scanf("%d", &znach); // вводим значение
  52.  
  53.     if (znach < result[0])
  54.       {
  55.         result[0] = znach;
  56.         sift (result, 0, k-1);
  57.       }
  58.   }
  59.  
  60. printf ("%d", result[0]);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement