#include<bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
signed main() {
int n; cin >> n;
stack<int> st;
int ans = 0;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
if (st.empty()) {
st.push(x);
ans++;
continue;
}
while (!st.empty() && st.top() > x) {
// decreasing; for example 3,2 -> need to end 3's layer because the 3 can't match another 3 to the right anymore
st.pop();
}
// if equal, then don't need to increment this one
if (!st.empty() && st.top() == x) {
continue;
}
st.push(x); // create a new layer for us
ans++;
}
// the left-over ones also need to be popped
while (!st.empty()) {
st.pop();
ans++;
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |