Advertisement
Dang_Quan_10_Tin

CIRCLES

Aug 19th, 2022 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #define task "CIRCLES"
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. constexpr int N = 1e4 + 5;
  13. constexpr ll Inf = 1e17;
  14. int n, f[N], dp[N], last[N];
  15. struct Circle
  16. {
  17.     ll l, r;
  18. } a[N];
  19.  
  20. void Read()
  21. {
  22.     cin >> n;
  23.  
  24.     for (int i = 1; i <= n; ++i)
  25.     {
  26.         ll x, r;
  27.         cin >> x >> r;
  28.  
  29.         a[i].l = x - r;
  30.         a[i].r = x + r;
  31.     }
  32. }
  33.  
  34. void Solve()
  35. {
  36.     a[0].l = -Inf;
  37.     a[0].r = Inf;
  38.  
  39.     sort(a, a + n + 1, [&](const Circle &x, const Circle &y)
  40.          { return x.r < y.r || (x.r == y.r && x.l > y.l); });
  41.  
  42.     for (int i = 0; i <= n; ++i)
  43.     {
  44.         int l = 0, mid, h = i - 1;
  45.         while (l <= h)
  46.         {
  47.             mid = (l + h) / 2;
  48.             if (a[mid].r <= a[i].l)
  49.                 l = mid + 1;
  50.             else
  51.                 h = mid - 1;
  52.         }
  53.  
  54.         last[i] = h;
  55.     }
  56.  
  57.     for (int i = 0; i <= n; ++i)
  58.     {
  59.         for (int j = 0; j < i; ++j) // assume a[j].r <= a[i].r
  60.             if (a[i].l <= a[j].l)
  61.             {
  62.                 dp[j + 1] = dp[j];
  63.                 dp[j + 1] = max(dp[j + 1], dp[last[j] + 1] + f[j]);
  64.             }
  65.             else
  66.                 dp[j + 1] = dp[j];
  67.  
  68.         f[i] = dp[i] + 1;
  69.     }
  70.  
  71.     cout << f[n] - 1;
  72. }
  73.  
  74. int32_t main()
  75. {
  76.     ios_base::sync_with_stdio(0);
  77.     cin.tie(0);
  78.     cout.tie(0);
  79.  
  80.     if (fopen(task ".INP", "r"))
  81.     {
  82.         freopen(task ".INP", "r", stdin);
  83.         freopen(task ".OUT", "w", stdout);
  84.     }
  85.  
  86.     Read();
  87.     Solve();
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement