一、题目链接
二、题目分析
(一)算法标签
模拟
(二)解题思路
见四、其它题解
三、AC代码
解法一:
unique函数用法
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5e5 + 10;
int a[N];
int n, M = 0;
int cnt[10010];
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ )
{
cin >> a[i];
M = max(M, a[i]);
}
// 去除相邻相同元素(只保留一个)
n = unique(a + 1, a + n + 1) - (a + 1);
// 初始化
a[0] = a[n + 1] = 0;
for (int i = 1; i <= n; i ++ )
{
int x = a[i - 1], y = a[i], z = a[i + 1];
if (y > x && y > z) // 凸峰
cnt[y] ++ ;
else if (y < x && y < z)
cnt[y] -- ;
}
int res = 0, sum = 0;
for (int i = M; i; i -- )
{
sum += cnt[i];
res = max(res, sum);
}
cout << res;
return 0;
}