fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N=1e6+6;
  5. int n, a[N], ans[N];
  6. stack<int> st;
  7.  
  8. signed main()
  9. {
  10. ios::sync_with_stdio(0);
  11. cin.tie(0); cout.tie(0);
  12.  
  13. cin >> n;
  14. for(int i=1;i<=n;++i) cin >> a[i];
  15. for(int i=1;i<=n;++i){
  16. while(!st.empty() && a[st.top()]<a[i]){
  17. ans[st.top()]=i-st.top()-1;
  18. st.pop();
  19. }
  20. st.push(i);
  21. }
  22. while(!st.empty()){
  23. ans[st.top()]=n-st.top();
  24. st.pop();
  25. }
  26. for(int i=1;i<=n;++i) cout << ans[i] << ' ';
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty