#pragma GCC optimize("O3", "unroll-loops") // God Help me !! #include using namespace std; #include #include #define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout) #define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define FIXED cout << fixed << setprecision(20) #define RANDOM srand(time(nullptr)) #define sz(a) (int)a.size() #define int long long #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++) #define sep(i,a,b) for(int i=(int)a;i>=(int)b;i--) #define inf 0x3f3f3f3f #define mp make_pair #define pb push_back #define eb emplace_back #define pf push_front #define popb pop_back #define popf pop_front #define lb lower_bound #define ub upper_bound #define all(a) a.begin(),a.end() #define ff first #define ss second #define endl "\n" #define n6 3000005 #define n3 3005 #define n5 300005 #define pi pair #define pii pair template using ordered_set = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // find_by_order(), order_of_key() /*******************************************************************************//*******************************************************************************/ #define dbg1(x) cout << #x << ": " << x << endl; #define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl; #define dbg3(x, y, z) cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl; #define dbg4(a, b, c, d) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl; #define dbg5(a, b, c, d, e) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define dbg6(a, b, c, d, e, f) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl; #define dbg(...) fprintf(stderr, __VA_ARGS__) #define dbgv(x) cout << #x << " = " << x << endl #define dbga(arr, len) {cout << #arr << " = "; for (int _ = 0; _ < len; _++)cout << arr[_] << " "; cout << endl;} #define dbgi(it) {cout << #it << " = "; for (const auto& _ : it)cout << _ << " "; cout << endl;} /*******************************************************************************//*******************************************************************************/ template inline void ckmax(T &x, T y) {if (y > x) x = y; } template inline void ckmin(T &x, T y) {if (y < x) x = y; } #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator _it(_ss); err(_it, args); } void err(istream_iterator it) {} template void err(istream_iterator it, T a, Args... args) { cout << *it << " =: " << a << endl; err(++it, args...); } template inline std::ostream& operator << (std::ostream& os, const std::pair& buf) { return os << "(" << buf.first << ": " << buf.second << ")"; } template inline std::ostream &operator << (std::ostream & os,const std::vector& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template inline std::ostream &operator << (std::ostream & os,const std::set& v) { bool first = true; os << "{"; for (typename std::set::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "}"; } template inline std::ostream &operator << (std::ostream & os,const std::map& v) { bool first = true; os << "{"; for (typename std::map::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "}"; } template inline std::ostream &operator << (std::ostream & os,const vector>& v) { os << '\n'; for (auto &u: v) os << u << '\n'; return os; } const int MOD = 1e9 + 7; const long long INF = 1e18; const double EPS = 1e-6; /*******************************************************************************//*******************************************************************************/ inline int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } inline int sub(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } inline int mul(int a, int b) { return (long long) a * b % MOD; } inline int power(int a, long long b) { int res = 1; while (b > 0) { if (b & 1) { res = mul(res, a); } a = mul(a, a); b >>= 1; } return res; } /*******************************************************************************//*******************************************************************************/ void showdq(deque g) { for (auto it = g.begin(); it != g.end(); ++it) cout << *it << ' ' ; cout << '\n'; } void showstack(stack s) { while(!s.empty()){ cout << s.top() << ' '; s.pop(); } cout << '\n'; } void showqueue(queue q) { while(!q.empty()){ cout << q.front() << ' '; q.pop(); } cout << '\n'; } int solve_util(deque &q){ } void nextGreater(int arr[], int n, int next[], char order) { stack S; for (int i=n-1; i>=0; i--) { // Keep removing top element from S while the top // element is smaller then or equal to arr[i] (if Key is G) // element is greater then or equal to arr[i] (if order is S) while (!S.empty() && ((order=='G')? arr[S.top()] <= arr[i]: arr[S.top()] >= arr[i])) S.pop(); // store the next greater element of current element if (!S.empty()) next[i] = S.top(); // If all elements in S were smaller than arr[i] else next[i] = -1; // Push this element S.push(i); } } void nextSmallerOfNextGreater(int arr[], int n) { int NG[n]; // stores indexes of next greater elements int RS[n]; // stores indexes of right smaller elements nextGreater(arr, n, NG, 'G'); nextGreater(arr, n, RS, 'S'); for (int i=0; i< n; i++) { if (NG[i] != -1 && RS[NG[i]] != -1) cout << arr[RS[NG[i]]] << " "; else cout<<"-1"<<" "; } } int l[n6], r[n6], arr[n6]; // stackst; int n; void solve(){ cin >> n ; rep(i,0,n-1){ cin>>arr[i]; } nextSmallerOfNextGreater(arr, n); } signed main() { FAST; FIXED; RANDOM; int t=1; // cin>>t; // time_t time_t1, time_t2; // time_t1 = clock(); while(t--) solve(); // time_t2 = clock(); // cout << "time taken :" << time_t2 - time_t1 << endl; return 0; }