fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, a[1000004], ret[1000004];
  4. stack<int> stk;
  5. int main(){
  6. cin >> n;
  7. for(int i = 0; i < n; i++){
  8. cin >> a[i];
  9. }
  10. for(int i = 0; i < n; i++){
  11. while(stk.size() && a[stk.top()] < a[i]){
  12. ret[stk.top()] = a[i];
  13. stk.pop();
  14. }
  15. stk.push(i);
  16. }
  17.  
  18. for(int i = 0; i < n; i++){
  19. cout << ret[i] << ' ';
  20. }
  21. }
Success #stdin #stdout 0.01s 5736KB
stdin
4
3 5 2 7
stdout
5 7 7 0