fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8. int n;
  9. cin >> n;
  10.  
  11. vector<int> a(n);
  12.  
  13. for(int i = 0; i < n; i++) cin >> a[i];
  14.  
  15. stack<int> s;
  16. vector<int> ans(n);
  17. for(int i = n - 1; i >= 0; i--){
  18. while(!s.empty() && s.top() < a[i]){
  19. s.pop();
  20. }
  21. if(s.empty()) ans[i] = -1;
  22. else ans[i] = s.top();
  23. s.push(a[i]);
  24. }
  25.  
  26. for(auto i : ans) cout << i << " ";
  27. }
Success #stdin #stdout 0.01s 5556KB
stdin
4
7 7 7 7
stdout
7 7 7 -1