Advertisement
tien_noob

POST - VNUOI 2021

Feb 4th, 2022
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. //Make CSP great again
  2. //You're as beautiful as the day I lost you
  3. //New year, best wishes
  4. #include <bits/stdc++.h>
  5. #define TASK "TESTCODE"
  6. #define Log2(x) 31 - __builtin_clz(x)
  7. using namespace std;
  8. const int N = 1e5;
  9. int n, a[N + 3];
  10. void read()
  11. {
  12.     cin >> n >> a[1] >> a[2];
  13.     for (int i = 3; i <= n + 2; ++ i)
  14.     {
  15.         cin >> a[i];
  16.     }
  17. }
  18. bool check(int k)
  19. {
  20.     set<int> dp = {a[1]};
  21.     set<int> :: iterator it;
  22.     for (int i = 2; i < n + 2; ++ i)
  23.     {
  24.         if (dp.empty())
  25.         {
  26.             return false;
  27.         }
  28.         if (abs(a[i] - a[i + 1]) <= k)
  29.         {
  30.             dp.insert(a[i]);
  31.         }
  32.         while(!dp.empty() && a[i + 1] - *dp.begin() > k)
  33.         {
  34.             it = dp.begin();
  35.             dp.erase(it);
  36.         }
  37.         while(!dp.empty() && *dp.rbegin() - a[i + 1] > k)
  38.         {
  39.             it = dp.end();
  40.             --it;
  41.             dp.erase(it);
  42.         }
  43.     }
  44.     return !dp.empty();
  45. }
  46. void solve()
  47. {
  48.     int low = abs(a[2] - a[1]), high = 1e9;
  49.     while(low <= high)
  50.     {
  51.         int mid = (low + high)/2;
  52.         if (check(mid))
  53.         {
  54.             high = mid - 1;
  55.         }
  56.         else
  57.         {
  58.             low = mid + 1;
  59.         }
  60.     }
  61.     cout << low;
  62. }
  63. int main()
  64. {
  65.     ios_base::sync_with_stdio(false);
  66.     cin.tie(nullptr);
  67.     if (fopen(TASK".INP", "r"))
  68.     {
  69.         freopen(TASK".INP", "r", stdin);
  70.         //freopen(TASK".out", "w", stdout);
  71.     }
  72.     int t = 1;
  73.     bool typetest = false;
  74.     if (typetest)
  75.     {
  76.         cin >> t;
  77.     }
  78.     for (int __ = 1; __ <= t; ++ __)
  79.     {
  80.         //cout << "Case " << __ << ": ";
  81.         read();
  82.         solve();
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement