题目
给出一个长度为 的由正整数构成的序列,你需要从中删除一个正整数,很显然你有很多种删除方式,你需要对删除这个正整数以后的序列求其最长上升子串,请问在所有删除方案中,最长的上升子串长度是多少。
这里给出最长上升子串的定义:即对于序列中连续的若干个正整数,满足 ,则称这连续的若干个整数构成的子串为上升子串,在所有的上升子串中,长度最长的称为最长上升子串。
输入格式
输入第一行仅包含一个正整数 ,表示给出的序列的长度。
接下来一行有 个正整数,即这个序列,中间用空格隔开。
输出格式
输出仅包含一个正整数,即删除一个数字之后的最长上升子串长度。
数据范围
,
输入样例:
5
2 1 3 2 5
输出样例:
3
题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <iostream> #include <cstring>
using namespace std;
const int N = 100010;
int f[N], g[N], a[N]; int n;
int main() { scanf("%d", &n); for (int i = 1; i <= n; ++ i) scanf("%d", &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;
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, f[i - 1] + g[i + 1]); else res = max(res, max(f[i - 1], g[i - 1]));
printf("%d\n", res);
return 0; }
|