Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define el endl
- #define umi unordered_map<int, int>
- #define umll unordered_map<ll, ll>
- #define all(vect) vect.begin(), vect.end()
- #define reset(A) memset(A, 0, sizeof(A))
- #define approx(n) fixed << setprecision(n)
- const int mod = 1e9 + 7;
- using namespace std;
- void solve()
- {
- int n;
- cin >> n;
- int a[n];
- umi mp;
- for(int i = 0; i < n; i++)
- {
- cin >> a[i];
- mp[a[i]] = i;
- }
- stack<int> st, s1;
- int b[n], c[n];
- for(int i = n - 1; i >= 0; i--)
- {
- if(st.empty())
- {
- st.push(a[i]);
- b[i] = -1;
- }
- else
- {
- while(!st.empty() && st.top() <= a[i])
- st.pop();
- if(st.empty())
- {
- b[i] = -1;
- }
- else
- {
- b[i] = st.top();
- }
- st.push(a[i]);
- }
- }
- // for(int i = 0; i < n; i++)
- // cout << b[i] << " ";
- // cout << el << el;
- // for(int i = 0; i < n; i++)
- // {
- // 5 1 9 2 5 1 7
- // 9 9 -1 5 7 7 -1
- // }
- for(int i = n - 1; i >= 0; i--)
- {
- if(s1.empty())
- {
- s1.push(a[i]);
- c[i] = -1;
- }
- else
- {
- while(!s1.empty() && s1.top() >= a[i])
- s1.pop();
- if(s1.empty())
- {
- c[i] = -1;
- }
- else
- {
- c[i] = s1.top();
- }
- s1.push(a[i]);
- }
- }
- for(int i = 0; i < n; i++)
- {
- if(b[i] != -1)
- {
- cout << c[mp[b[i]]] << " ";
- }
- else
- cout << -1 << " ";
- }
- cout << el;
- }
- int main()
- {
- int t = 1;
- cin >> t;
- // cin.ignore();
- while(t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement