Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n;
- cin>>n;
- int arr[n+1];
- for(int i=1; i<=n; i++)
- cin>>arr[i];
- stack <pair<int,int>> s;
- vector<pair<int,int>>vec;
- s.push({1,arr[1]});
- for (int i = 2; i <= n; i++)
- {
- if (s.empty())
- {
- s.push({i,arr[i]});
- continue;
- }
- while (s.empty() == false && s.top().second < arr[i])
- {
- vec.push_back({s.top().first,i});
- s.pop();
- }
- s.push({i,arr[i]});
- }
- while (s.empty() == false)
- {
- vec.push_back({s.top().first,-1});
- s.pop();
- }
- sort(vec.begin(),vec.end());
- for(int i=0;i<vec.size();i++)
- {
- if(vec[i].second==-1)
- cout<<-1<<" ";
- else cout<<(vec[i].second-vec[i].first)<<" ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement