vadimk772336

Untitled

Oct 9th, 2021 (edited)
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 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 dot
  9. {
  10.     int x;
  11.     int y;
  12. };
  13.  
  14. struct center_status
  15. {
  16.     double x;
  17.     int status;
  18. };
  19.  
  20. void simpleSort(center_status* states, int l, int r)
  21. {
  22.     center_status buff;
  23.     int j;
  24.  
  25.     for (int i = l + 1; i < r + 1; ++i)
  26.     {
  27.         j = i;
  28.         while (j > l && states[j - 1].x > states[j].x)
  29.         {
  30.             buff = states[j - 1];
  31.             states[j - 1] = states[j];
  32.             states[j] = buff;
  33.             j--;
  34.         }
  35.     }
  36. }
  37.  
  38. void merge(center_status* states, int l, int mid, int r)
  39. {
  40.     int ls = 0, rs = 0;
  41.     center_status result[r - l + 1];
  42.  
  43.  
  44.     while ((l + ls < mid + 1) && (mid + rs < r))
  45.     {
  46.         if (states[l + ls].x < states[mid + rs + 1].x)
  47.         {
  48.             result[ls + rs] = states[l + ls];
  49.             ls++;
  50.         }
  51.         else
  52.         {
  53.             result[ls + rs] = states[mid + 1 + rs];
  54.             rs++;
  55.         }
  56.     }
  57.  
  58.  
  59.     while (l + ls < mid + 1)
  60.     {
  61.         result[ls + rs] = states[l + ls];
  62.         ls++;
  63.     }
  64.  
  65.     while (mid + rs < r)
  66.     {
  67.         result[ls + rs] = states[mid + rs + 1];
  68.         rs++;
  69.     }
  70.  
  71.     for (int i = 0; i < ls + rs; ++i)
  72.         states[l + i] = result[i];
  73. }
  74.  
  75. void mergeSort(center_status* states, int l, int r)
  76. {
  77.     if (r - l <= 1)
  78.         return;
  79.  
  80.     if (r - l < 12)
  81.     {
  82.         simpleSort(states, l, r);
  83.         return;
  84.     }
  85.  
  86.     int mid = (l + r) / 2;
  87.     mergeSort(states, l, mid);
  88.     mergeSort(states, mid + 1, r);
  89.     merge(states, l, mid, r);
  90. }
  91.  
  92. bool is_contain_k(double radius, dot* dots, int n, int k)
  93. {
  94.     double eps = 1e-6;
  95.     double offset;
  96.     center_status states[2 * n];
  97.  
  98.     for (int i = 0; i < n; ++i)
  99.     {
  100.         if (abs(dots[i].y) < radius)
  101.         {
  102.             offset = sqrt(radius * radius - dots[i].y * dots[i].y);
  103.             states[2 * i].x = dots[i].x - offset - eps * (i + 1);
  104.             states[2 * i].status = 1;
  105.             states[2 * i + 1].x = dots[i].x + offset + eps * (i + 1);
  106.             states[2 * i + 1].status = 0;
  107.         }
  108.         else
  109.         {
  110.             states[2 * i].x = 0;
  111.             states[2 * i].status = 2;
  112.             states[2 * i + 1].x = 0;
  113.             states[2 * i + 1].status = 2;
  114.         }
  115.     }
  116.  
  117.     mergeSort(states, 0, 2 * n - 1);
  118.  
  119.     /*
  120.     for (int i = 0; i < 2 * n; ++i)
  121.     {
  122.         cout << states[i].x << " ";
  123.     }
  124.     cout << endl;
  125.     cout << endl;
  126.     */
  127.  
  128.     int curr_count = 0;
  129.     int max_count = 0;
  130.     for (int i = 0; i < 2 * n; ++i)
  131.     {
  132.         if (states[i].status == 1)
  133.         {
  134.  
  135.             curr_count++;
  136.  
  137.             if (curr_count > max_count)
  138.                 max_count = curr_count;
  139.         }
  140.         if (states[i].status == 0)
  141.         {
  142.             curr_count--;
  143.         }
  144.     }
  145.  
  146.     if (max_count >= k)
  147.         return true;
  148.     return false;
  149. }
  150.  
  151. double binary_search(dot* dots, int n, int k)
  152. {
  153.     double max_r = 1420;
  154.     double min_r = 0;
  155.     double radius;
  156.  
  157.     while (max_r - min_r > 1e-4)
  158.     {
  159.         radius = (max_r + min_r) / 2;
  160.  
  161.         if (is_contain_k(radius, dots, n, k))
  162.             max_r = radius;
  163.         else
  164.             min_r = radius;
  165.     }
  166.     return max_r;
  167. }
  168.  
  169. int main()
  170. {
  171.     int n, k;
  172.     double res;
  173.     int count_tests;
  174.  
  175.  
  176.     ifstream ifs("123.txt");
  177.     ifs >> count_tests;
  178.  
  179.     ofstream out("results.txt");
  180.     for (int i = 0; i < count_tests; ++i)
  181.     {
  182.  
  183.         ifs >> n;
  184.         ifs >> k;
  185.         dot dots[n] = {};
  186.  
  187.         for (int i = 0; i < n; ++i)
  188.         {
  189.             ifs >> dots[i].x;
  190.             ifs >> dots[i].y;
  191.             cout << dots[i].x << " " << dots[i].y << " \n";
  192.         }
  193.  
  194.  
  195.         res = binary_search(dots, n, k);
  196.         std::cout << std::setprecision(7) << res;
  197.         out << std::setprecision(7) << res;
  198.         out << '\n';
  199.     }
  200.     return 0;
  201. }
  202.  
Add Comment
Please, Sign In to add comment