AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 2014. 岛 0.05 AC币

作者: 作者的头像   Yipxx ,  2022-01-13 16:53:41 ,  所有人可见 ,  阅读 28


0


#include <iostream>
#include <cstring>
#include <algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

const int N = 100010;

int n;
int h[N];
PII q[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;    // 后续代码可能会用到第n + 1个位置,需要把第n + 1个位置清空

    for (int i = 1; i <= n; i ++ )
        q[i] = {h[i], i};

    sort(q + 1, q + n + 1);

    int res = 1, cnt = 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;
}

0 评论

你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息