Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0);
- int n,ans=0,cur=0;
- cin>>n;
- int a[n],next[n];
- for (int i=0;i<n;++i)
- cin>>a[i];
- stack<int> s;
- for (int i=n-1;i>=0;--i)
- {
- while (!s.empty()&&a[s.top()]<a[i])
- s.pop();
- if (s.empty())
- next[i]=n;
- else
- next[i]=s.top();
- s.push(i);
- }
- while (cur<n)
- {
- ++ans;
- cur=next[cur];
- }
- cout<<ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement