最长连续上升字串

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

using namespace std;

const int N = 100010;

int n;
int a[N], f[N], g[N];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);

    // 预处理f[i]:以i结尾的单调上升子串的最大长度
    for (int i = 1; i <= n; i ++ )
        if (a[i] > a[i - 1]) f[i] = f[i - 1] + 1;
        else f[i] = 1;

    // 预处理g[i]:以i开头的单调上升子串的最大长度
    for (int i = n; i; i -- )
        if (a[i] < a[i + 1]) g[i] = g[i + 1] + 1;
        else g[i] = 1;

    int res = 0;
    // 枚举删除哪个数
    for (int i = 1; i <= n; i ++ )
        if (a[i - 1] >= a[i + 1]) res = max(res, max(f[i - 1], g[i + 1]));
        else res = max(res, f[i - 1] + g[i + 1]);

    printf("%d\n", res);

    return 0;
}
posted @ 2022-05-17 20:40  兮何其  阅读(57)  评论(0)    收藏  举报