fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n ; cin>>n;
  7. vector<int>arr(n);
  8. for(int i = 0 ; i<n;i++){
  9. cin>>arr[i];
  10. }
  11. stack<int>s;
  12. vector<int>nge(n,0);
  13. for(int i = 0 ; i<n;i++){
  14. while(!s.empty() && arr[i]<0 && abs(arr[i])>abs(arr[s.top()])){
  15. nge[s.top()]=i;
  16. s.pop();
  17. }
  18. if(arr[i]>0) s.push(i);
  19.  
  20. }
  21. // your code goes here
  22. for(int i = 0 ; i<n;i++)cout<<nge[i]<<" ";
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 5324KB
stdin
6
5 9 -2 -4 6 -8
stdout
0 0 0 0 5 0