artemgf

Медиана последовательности

Nov 9th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stdio.h>
  9. #include <cmath>
  10. #include <math.h>
  11. #include <queue>
  12. #include <stack>
  13. #include <climits>
  14. #include <deque>
  15. #include <ctime>
  16.  
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. typedef unsigned int ui;
  22.  
  23. #define mh() make_heap()
  24. #define poph() pop_heap()
  25. #define pushh() push_heap()
  26. #define sor(n) n.begin(), n.end()
  27.  
  28. int main()
  29. {
  30.     int n;
  31.     scanf("%d", &n);
  32.     vector <unsigned int> h;
  33.     h.reserve(n / 2 + 2);
  34.     unsigned int prom;
  35.     for (int i = 0; i <= n / 2; i++)
  36.     {
  37.         scanf("%ud", &prom);
  38.         h.push_back(prom);
  39.     }
  40.  
  41.     make_heap(sor(h));
  42.     for (int i = n / 2 + 1; i < n; i++)
  43.     {
  44.         scanf("%ud", &prom);
  45.         if (prom < h[0])
  46.         {
  47.             pop_heap(sor(h));
  48.             h.pop_back();
  49.             h.push_back(prom);
  50.             push_heap(sor(h));
  51.         }
  52.     }
  53.  
  54.     if (n % 2 == 0)
  55.     {
  56.         prom = h[0];
  57.         pop_heap(sor(h));
  58.         prom += h[0];
  59.         printf("%.1f", prom/2.0);
  60.     }
  61.     else
  62.     {
  63.         printf("%.1f", h[0]*1.0);
  64.     }
  65.     system("pause");
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment