#include <iostream> //https://www.luogu.com.cn/article/7xgb8awn
using namespace std;
const int maxn = 300005;
int n, a[maxn], f[maxn], top1, top2, stk1[maxn], stk2[maxn];
int main() {
cin.tie(0); ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
while(top1 && a[stk1[top1] <= a[i]]) top1--;
stk1[++top1] = i;
while(top2 && a[stk2[top2]] > a[i]) top2--;
stk2[++top2] = i;
f[i] = f[*lower_bound(stk2 + 1, stk2 + top2 + 1, stk1[top1 - 1]) - 1] + 1;
}
cout << f[n];
}
#include<bits/stdc++.h>
using namespace std;
int n,a[300005],dp[300005],top1,top2,stk1[300005],stk2[300005];
//stk1:严格单调递减栈 stk2:单调递增栈(使用手写栈的原因是时间过于紧张)
int main()
{
scanf("%d",&n);
for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
for(int i = 1; i <= n; i++)
{
while(top1 && a[stk1[top1]] <= a[i]) top1--;
stk1[++top1] = i;
while(top2 && a[stk2[top2]] > a[i]) top2--;
stk2[++top2] = i;
//维护两个单调栈
dp[i] = dp[*lower_bound(stk2 + 1,stk2 + top2 + 1,stk1[top1 - 1]) - 1] + 1;
//千辛万苦得出的状态转移方程
}
printf("%d",dp[n]);
return 0;
}
这两段代码有什么不同吗,我以凡人之躯没看出什么不同