给出一个长度为 nn 的由正整数构成的序列,你需要从中删除一个正整数,很显然你有很多种删除方式,你需要对删除这个正整数以后的序列求其最长上升子串,请问在所有删除方案中,最长的上升子串长度是多少。
这里给出最长上升子串的定义:即对于序列中连续的若干个正整数,满足 ai+1>aiai+1>ai,则称这连续的若干个整数构成的子串为上升子串,在所有的上升子串中,长度最长的称为最长上升子串。
输入格式
输入第一行仅包含一个正整数 nn,表示给出的序列的长度。
接下来一行有 nn 个正整数,即这个序列,中间用空格隔开。
输出格式
输出仅包含一个正整数,即删除一个数字之后的最长上升子串长度。
数据范围
1≤n≤1051≤n≤105,
1≤ai≤1051≤ai≤105
输入样例:
输出样例:
3
#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 100010;int a[N], f[N], g[N];int n;int main() {cin >> n;for (int i = 1; i <= n; ++i) cin >> a[i];//f[i] 以a[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] 以a[i]开始的最大上升子串长度for (int i = n; i; --i) {if (a[i + 1] > a[i]) 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]);}cout << res << endl;return 0;}
