Eileanora

Untitled

Mar 25th, 2023
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. #define Num_of_Digits(n) ((int)log10(n) + 1)
  6. #define sz(x) int(x.size())
  7. #define all(vec) vec.begin(), vec.end()
  8. #define rall(vec) vec.rbegin(), vec.rend()
  9. #define fixed(n) fixed << setprecision(n)
  10. #define ll long long
  11. #define ull unsigned long long
  12. constexpr ll linf = 1LL << 62;
  13. constexpr int inf = 1 << 30;
  14. constexpr int mod = 1e9 + 7;
  15. #define PI acos(-1)
  16. #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
  17. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0)
  18. #define PI acos(-1)
  19. #define multiOrderedSet tree <int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  20. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  21.  
  22. using namespace std;
  23. using namespace __gnu_pbds;
  24. template <typename T = int> istream &operator>>(istream &in, vector<T> &v)
  25. {
  26.     for (auto &x : v)
  27.         in >> x;
  28.     return in;
  29. }
  30. template <typename T = int> ostream &operator<<(ostream &out, const vector<T> &v)
  31. {
  32.     for (const T &x : v)
  33.         out << x << " ";
  34.     return out;
  35. }
  36.  
  37. void set_file(string &file_name) {
  38.     freopen((file_name + ".in").c_str(), "r", stdin);
  39.     freopen((file_name + ".out").c_str(), "w", stdout);
  40. }
  41.  
  42. void btats()
  43. {
  44.     ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  45.     #ifndef ONLINE_JUDGE
  46.         freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  47.     #endif
  48.     //freopen("input.txt", "r", stdin);
  49.     //freopen("output.txt", "w", stdout);
  50. }
  51.  
  52. void solve()
  53. {
  54.     int n;
  55.     cin >> n;
  56.     if (n == 1) {
  57.         cout << 0;
  58.         return;
  59.     }
  60.    
  61.     vector <int> v(n) , dist(n + 1);
  62.     for (int i = 0; i < n; i++) {
  63.         cin >> v[i];
  64.         dist[v[i]] = i;
  65.     }
  66.  
  67.     stack <int> monotonic;
  68.     vector <int> ans(n, -1);
  69.     for(int i = 0; i < n; i++)
  70.     {
  71.         while(!monotonic.empty() && monotonic.top() < v[i]) {
  72.             monotonic.pop();
  73.         }
  74.  
  75.         ans[i] = monotonic.empty() ? -1 : abs(dist[monotonic.top()] - i);
  76.         monotonic.push(v[i]);
  77.     }
  78.     monotonic = stack <int> ();
  79.     for(int i = n - 1; i >= 0; i--) {
  80.         while(!monotonic.empty() && monotonic.top() < v[i]) {
  81.             monotonic.pop();
  82.         }
  83.  
  84.         if(!monotonic.empty())
  85.             ans[i] = (ans[i] == -1) ? abs(dist[monotonic.top()] - i) : min(ans[i], abs(dist[monotonic.top()] - i));
  86.         monotonic.push(v[i]);
  87.     }
  88.     cout << *max_element(all(ans));
  89. }
  90.  
  91. int main()
  92. {
  93.     btats();
  94.     int test = 1;
  95.     cin >> test;
  96.     while(test--)
  97.     {
  98.         solve();
  99.         cout << "\n";
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment