little shino (101)
unknown
c_cpp
3 years ago
993 B
5
Indexable
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; stack<int> st; int n; int a[maxn],nxt[maxn],prv[maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(int i=0;i<n;i++){ cin >> a[i]; } st.push(n-1); nxt[n-1] = -1; prv[0] = -1; for(int i=n-2;i+1;i--){ while(!st.empty() && a[st.top()] <= a[i]){ st.pop(); } if(!st.empty()){ nxt[i] = st.top(); } else{ nxt[i] = -1; } st.push(i); } while(!st.empty()){ st.pop(); } st.push(0); for(int i=1;i<n;i++){ while(!st.empty() && a[st.top()] <= a[i]){ st.pop(); } if(!st.empty()){ prv[i] = st.top(); } else{ prv[i] = -1; } st.push(i); } long long ans = 0; int end,strt; for(int i=0;i<n;i++){ if(nxt[i] != -1){ ans += 1; } if(prv[i] != -1){ ans += 1; } } cout << ans; }
Editor is loading...