Guest User

Untitled

a guest
Feb 4th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include<vector>
  3. #include<stack>
  4.  
  5. using namespace std;
  6. void stackSpan(int *a, int n){
  7.     stack <int> s;
  8.     int b[n];
  9.     for(int i = 0; i < n; i++){
  10.        // cout<<"Hello"<<endl;
  11.        //Why on using s.empty() my program gets segmentation error every time
  12.         while(s.top() < a[i] && !s.empty()){
  13.             s.pop();
  14.         }
  15.        int better = s.empty() ? i + 1: i - s.top();
  16.        s.push(i);
  17.        b[i] = better;
  18.            
  19.     }
  20.     for(int i = 0; i < n; i++){
  21.         cout<<b[i]<<" ";
  22.        
  23.     }
  24.     cout<<"END";
  25. }
  26.  
  27. int main(){
  28.     int n;
  29.     cin>>n;
  30.     int a[n];
  31.    
  32.     stackSpan(a, n);
  33.     return 0;
  34. }
Add Comment
Please, Sign In to add comment