fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. int n;
  9. cin>>n;
  10.  
  11. int a[n];
  12. for(int i=0;i<n;i++)
  13. {
  14. cin>>a[i];
  15. }
  16.  
  17. stack<int> St;
  18. vector<int> ans;
  19. for(int i=0;i<n;i++)
  20. {
  21. while(!St.empty() && a[St.top()] >= a[i])
  22. {
  23. St.pop();
  24. }
  25.  
  26. if(St.empty())
  27. {
  28. ans.push_back(0);
  29. }
  30. else
  31. {
  32. ans.push_back(St.top()+1);
  33. }
  34.  
  35. St.push(i);
  36. }
  37.  
  38. for(int i=0;i<ans.size();i++)
  39. {
  40. cout<<ans[i]<<" ";
  41. }
  42. cout<<endl;
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 5288KB
stdin
8
2 5 1 4 8 3 2 5
stdout
0 1 0 3 4 3 3 7