Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define Num_of_Digits(n) ((int)log10(n) + 1)
- #define sz(x) int(x.size())
- #define all(vec) vec.begin(), vec.end()
- #define rall(vec) vec.rbegin(), vec.rend()
- #define fixed(n) fixed << setprecision(n)
- #define ll long long
- #define ull unsigned long long
- constexpr ll linf = 1LL << 62;
- constexpr int inf = 1 << 30;
- constexpr int mod = 1e9 + 7;
- #define PI acos(-1)
- #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
- #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0)
- #define PI acos(-1)
- #define multiOrderedSet tree <int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
- #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
- using namespace std;
- using namespace __gnu_pbds;
- template <typename T = int> istream &operator>>(istream &in, vector<T> &v)
- {
- for (auto &x : v)
- in >> x;
- return in;
- }
- template <typename T = int> ostream &operator<<(ostream &out, const vector<T> &v)
- {
- for (const T &x : v)
- out << x << " ";
- return out;
- }
- void set_file(string &file_name) {
- freopen((file_name + ".in").c_str(), "r", stdin);
- freopen((file_name + ".out").c_str(), "w", stdout);
- }
- void btats()
- {
- ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- }
- void solve()
- {
- int n;
- cin >> n;
- if (n == 1) {
- cout << 0;
- return;
- }
- vector <int> v(n) , dist(n + 1);
- for (int i = 0; i < n; i++) {
- cin >> v[i];
- dist[v[i]] = i;
- }
- stack <int> monotonic;
- vector <int> ans(n, -1);
- for(int i = 0; i < n; i++)
- {
- while(!monotonic.empty() && monotonic.top() < v[i]) {
- monotonic.pop();
- }
- ans[i] = monotonic.empty() ? -1 : abs(dist[monotonic.top()] - i);
- monotonic.push(v[i]);
- }
- monotonic = stack <int> ();
- for(int i = n - 1; i >= 0; i--) {
- while(!monotonic.empty() && monotonic.top() < v[i]) {
- monotonic.pop();
- }
- if(!monotonic.empty())
- ans[i] = (ans[i] == -1) ? abs(dist[monotonic.top()] - i) : min(ans[i], abs(dist[monotonic.top()] - i));
- monotonic.push(v[i]);
- }
- cout << *max_element(all(ans));
- }
- int main()
- {
- btats();
- int test = 1;
- cin >> test;
- while(test--)
- {
- solve();
- cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment