Advertisement
Eternoseeker

171-div2-B

Oct 28th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. template <typename A, typename B>
  5. ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
  6. template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
  7. ostream &operator<<(ostream &os, const T_container &v)
  8. {
  9.     os << '{';
  10.     string sep;
  11.     for (const T &x : v)
  12.         os << sep << x, sep = ", ";
  13.     return os << '}';
  14. }
  15. void dbg_out() { cerr << endl; }
  16. template <typename Head, typename... Tail>
  17. void dbg_out(Head H, Tail... T)
  18. {
  19.     cerr << ' ' << H;
  20.     dbg_out(T...);
  21. }
  22. #ifdef LOCAL
  23. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  24. #else
  25. #define dbg(...)
  26. #endif
  27.  
  28. typedef long long ll;
  29. typedef pair<int, int> ii;
  30. typedef vector<int> vi;
  31. typedef vector<ii> vii;
  32.  
  33. #define ar array
  34. #define ld long double
  35. #define sza(x) ((int)x.size())
  36. #define all(a) (a).begin(), (a).end()
  37.  
  38. const int MAX_N = 1e5 + 5;
  39. const ll MOD = 1e9 + 7;
  40. const ll INF = 1e9;
  41. const ld EPS = 1e-9;
  42.  
  43. bool is_possible(int n, const vector<ll> &a, ll k){
  44.     vector<bool> dp0(n + 1, false);
  45.     vector<bool> dp1(n + 1, false);
  46.     dp0[0] = true;
  47.     dp1[0] = false;
  48.  
  49.     for (int i = 0; i < n; ++i){
  50.         vector<bool> next0(n + 1, false);
  51.         vector<bool> next1(n + 1, false);
  52.  
  53.         if (dp0[i]){
  54.             if (i + 1 < n && abs(a[i + 1] - a[i]) <= k){
  55.                 next0[i + 2] = true;
  56.             }
  57.             if (k >= 1){
  58.                 next1[i + 1] = true;
  59.             }
  60.         }
  61.  
  62.         if (dp1[i]){
  63.             if (i + 1 < n && abs(a[i + 1] - a[i]) <= k){
  64.                 next1[i + 2] = true;
  65.             }
  66.         }
  67.  
  68.         for (int j = 0; j <= n; ++j){
  69.             if (next0[j])
  70.                 dp0[j] = true;
  71.             if (next1[j])
  72.                 dp1[j] = true;
  73.         }
  74.     }
  75.  
  76.     return dp0[n] || dp1[n];
  77. }
  78.  
  79. void solve()
  80. {
  81.     int n;
  82.     cin >> n;
  83.     vector<ll> a(n);
  84.     for (auto &x : a)
  85.         cin >> x;
  86.  
  87.     ll left = 0, right = (ll)1e18;
  88.     ll answer = right;
  89.     while (left <= right){
  90.         ll mid = left + (right - left) / 2;
  91.         if (is_possible(n, a, mid)){
  92.             answer = mid;
  93.             right = mid - 1;
  94.         }
  95.         else{
  96.             left = mid + 1;
  97.         }
  98.     }
  99.  
  100.     cout << answer << "\n";
  101. }
  102.  
  103. int main()
  104. {
  105.     ios_base::sync_with_stdio(0);
  106.     cin.tie(0);
  107.     cout.tie(0);
  108.     int tc;
  109.     cin >> tc;
  110.     for (int t = 1; t <= tc; t++){
  111.         solve();
  112.     }
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement