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. ios_base::sync_with_stdio(false);
  7. cin.tie(NULL); cout.tie(NULL);
  8. cin >> n;
  9. for(int i = 0; i < n; i++){
  10. cin >> a[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 5664KB
stdin
4
3 5 2 7
stdout
5 7 7 0