暴力枚举
极值元素的拆分相邻元素的积一定异号
接下来看代码
C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int a[N], b[N];
inline void solve()
{
int n, res = 0;
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i < n; i ++ ) b[i] = a[i + 1] - a[i];
for (int i = 1; i < n; i ++ )
if (b[i] * b[i + 1] < 0 && i + 1 < n)
res ++;
cout << res << endl;
}
int main()
{
cin.tie(nullptr) -> sync_with_stdio(0);
solve();
return 0;
}