C++:(模拟)
#include<iostream>
#include<set>
#include<algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 500010;
int a[N];
PII q[N];
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
n = unique(a + 1, a + n + 1) - a - 1;
for(int i = 1; i <= n; i++){
q[i] = {a[i], i};
}
sort(q + 1, q + n + 1);
a[n + 1] = 0;
int res = 0, cnt = 1;
for(int i = 2; i <= n; i++){
int k = q[i - 1].y;
if(a[k - 1] < a[k] && a[k + 1] < a[k]) cnt--;
if(a[k - 1] > a[k] && a[k + 1] > a[k]) cnt++;
if(q[i - 1].x != q[i].x) res = max(res, cnt);
}
cout << res << endl;
}