Advertisement
Dang_Quan_10_Tin

TPS HSGHN L9 2022-2023

Jan 12th, 2023 (edited)
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. constexpr int N = 1e3 + 5;
  6. int n, k;
  7. int a[N * 2];
  8.  
  9. void Read()
  10. {
  11.     cin >> n;
  12.     for (int i = 1; i <= n; ++i)
  13.         cin >> a[i];
  14.  
  15.     cin >> k;
  16. }
  17.  
  18. bool Check(int R)
  19. {
  20.     for (int i = 1; i <= n; ++i)
  21.     {
  22.         int cntIntervals = 1;
  23.         int minInInterval = i;
  24.  
  25.         for (int j = i + 1; j < i + n; ++j)
  26.             if (a[j] - a[minInInterval] > R * 2)
  27.             {
  28.                 ++cntIntervals;
  29.                 minInInterval = j;
  30.             }
  31.  
  32.         // cntIntervals chính là số đoạn cần để phủ a[i…i+n-1]
  33.         if (cntIntervals <= k) // Chỉ tạo ra không quá k đoạn => Không dùng nhiều hơn k trạm phát sóng
  34.             return true;
  35.     }
  36.  
  37.     return false;
  38. }
  39.  
  40. void Solve()
  41. {
  42.     sort(a + 1, a + n + 1);
  43.  
  44.     for (int i = 1; i <= n; ++i)
  45.         a[i + n] = a[i] + 1000000;
  46.  
  47.     int l = 0, m, h = 1e6;
  48.     while (l <= h)
  49.     {
  50.         m = (l + h) / 2;
  51.         if (Check(m))
  52.             h = m - 1;
  53.         else
  54.             l = m + 1;
  55.     }
  56.  
  57.     cout << l;
  58. }
  59.  
  60. int32_t main()
  61. {
  62.     ios::sync_with_stdio(0);
  63.     cin.tie(0);
  64.     cout.tie(0);
  65.     Read();
  66.     Solve();
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement