AcWing 2014. 岛
原题链接
中等
c++
// h - l - h -> cnt ++ ;
// l - h - l -> cnt -- ;
// l1 - l2 - h -> 0;
// h1 - h2 - l -> 0;
// unique n;
// x, y;
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#define fors(i, a, b) for(int i = a; i < b; i ++ );
#define rep(i, a, b) for(int i = a; i <= b; i ++ );
#define per(i, a, b) for(int i = a; i >= b; i --);
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
ll res, cnt;
int n, m;
PII q[N];
int h[N];
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> h[i];
n = unique(h + 1, h + n + 1) - h - 1;
h[n + 1] = 0;
for(int i = 1; i <= n; i ++ ) q[i] = {h[i], i};
sort(q + 1, q + n + 1);
cnt = 1, res = 1;
for (int i = 1; i <= n; i ++ )
{
int k = q[i].y;
if (h[k - 1] < h[k] && h[k + 1] < h[k]) cnt -- ;
else if (h[k - 1] > h[k] && h[k + 1] > h[k]) cnt ++ ;
if (q[i].x != q[i + 1].x)
res = max(res, cnt);
}
cout << res << endl;
return 0;
}