vadimk772336

Untitled

Oct 10th, 2021
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. struct element
  9. {
  10.     double X;
  11.     int tip;
  12. };
  13.  
  14. struct tochka
  15. {
  16.     double X;
  17.     double Y;
  18. };
  19.  
  20. void merge(element* a, int left, int mid, int right)
  21. {
  22.     int i, it = 0;
  23.     int it_two = 0;
  24.     element result[right - left];
  25.  
  26.     while (left + it < mid and mid + it_two < right)
  27.     {
  28.         if (a[left + it].X < a[mid + it_two].X)
  29.         {
  30.             result[it + it_two] = a[left + it];
  31.             ++it;
  32.         }
  33.         else
  34.         {
  35.             result[it + it_two] = a[mid + it_two];
  36.             ++it_two;
  37.         }
  38.     }
  39.     while (left + it < mid)
  40.     {
  41.         result[it + it_two] = a[left + it];
  42.         ++it;
  43.     }
  44.     while (mid + it_two < right)
  45.     {
  46.         result[it + it_two] = a[mid + it_two];
  47.         ++it_two;
  48.     }
  49.  
  50.     for (i = 0; i < it + it_two; ++i)
  51.         a[left + i] = result[i];
  52. }
  53.  
  54. void mergeSort_struct(element* a, int n)
  55. {
  56.     int i, j;
  57.     for (i = 1; i < n; i *= 2)
  58.         for (j = 0; j < n - i; j += 2 * i)
  59.             merge(a, j, j + i, std::min(j + 2 * i, n));
  60. }
  61.  
  62. int Func(double r, int n, tochka* A)
  63. {
  64.     element E[2 * n];
  65.     double epsilon = 1e-6;
  66.     int i, deg = 0, len = 0;
  67.     for (i = 0; i < n; ++i)
  68.     {
  69.         if (abs(A[i].Y) < r)
  70.         {
  71.             E[2 * i].X = A[i].X - sqrt(r * r - (A[i].Y) * (A[i].Y)) - epsilon * (i + 1);
  72.             E[2 * i].tip = 1;
  73.             E[2 * i + 1].X = A[i].X + sqrt(r * r - (A[i].Y) * (A[i].Y)) + epsilon * (i + 1);
  74.             E[2 * i + 1].tip = 0;
  75.         }
  76.         else
  77.         {
  78.             E[2 * i].X = 0;
  79.             E[2 * i].tip = 2;
  80.             E[2 * i + 1].X = 0;
  81.             E[2 * i + 1].tip = 2;
  82.         }
  83.     }
  84.     mergeSort_struct(E, 2 * n);
  85.     for (i = 0; i < 2 * n; ++i)
  86.     {
  87.         if (deg < 0)
  88.             deg = 0;
  89.         if (E[i].tip == 1)
  90.         {
  91.             deg++;
  92.             if (deg > len)
  93.                 len = deg;
  94.         }
  95.         if (E[i].tip == 0)
  96.             deg--;
  97.     }
  98.     return len;
  99. }
  100.  
  101. double binary_search (tochka* A, int n, int k) {
  102.    
  103.     double L = 0,M;
  104.     double eps = 1e-4;
  105.     double max_y = 0.0;
  106.     for (int i = 0; i < n; ++i)
  107.     {
  108.         if (abs(A[i].Y) > max_y)
  109.         max_y = abs(A[i].Y);
  110.     }
  111.     double R = max_y + 1.0;
  112.    
  113.     while (R - L > eps)
  114.     {
  115.         M = (R + L) / 2;
  116.         if (Func(M, n, A) < k)
  117.             L = M;
  118.         else
  119.             R = M;
  120.     }
  121.     return R;
  122. }
  123.  
  124. int main()
  125. {
  126.     int n, k;
  127.     tochka A[n];
  128.     double res;
  129.    
  130.     int count_tests;
  131.  
  132.     ifstream ifs("123.txt");
  133.     ifs >> count_tests;
  134.  
  135.     ofstream out("results.txt");
  136.     for (int i = 0; i < count_tests; ++i)
  137.     {
  138.  
  139.         ifs >> n;
  140.         ifs >> k;
  141.         tochka A[n] = {};
  142.        
  143.         for (int i = 0; i < n; ++i)
  144.         {
  145.             ifs >> A[i].X;
  146.             ifs >> A[i].Y;
  147.             cout << A[i].X << " " << A[i].Y << " \n";
  148.         }
  149.    
  150.  
  151.         res = binary_search(A, n, k);
  152.         std::cout << std::setprecision(7) << res;
  153.         out << std::setprecision(7) << res;
  154.         out << '\n';
  155.  
  156.     }
  157.  
  158.     return 0;
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment