题目描述
不连续浏览海拔相同的两个景点,并且一旦开始下山,就不再向上走了
样例
数据范围
2≤N≤1000
输入样例:
8
186 186 150 200 160 130 197 220
输出样例:
4
算法1
(暴力枚举) $O(n^2)$
正着求一遍最长上升子序列,然后反着求一遍,再加起来
f[i] 表示以 a[i] 结尾的最长上升子序列
C++ 代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010;
int n;
int a[N], f[N], g[N];
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ )
{
f[i] = 1;
for (int j = 1; j < i; j ++ )
if (a[i] > a[j])
f[i] = max(f[i], f[j] + 1);
}
for (int i = n; i; i -- )
{
g[i] = 1;
for (int j = n; j > i; j -- )
if (a[i] > a[j])
g[i] = max(g[i], g[j] + 1);
}
int res = 0;
for (int i = 1; i <= n; i ++ )
res = max(res, f[i] + g[i] - 1);
// a[i] 重复了,减掉一个1
cout << res << endl;
return 0;
}