建议做这个题之前先学习一下LIS 的 nlogn 算法

注意二分查找时我们需要的是小于a【i】的数而不是等于甚至大于a【i】的数,当更新ans时发现貌似缺了1而无法理解代码时仔细考虑一下这一点

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

using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 200000 + 10;

int n, a[maxn], f[maxn], head[maxn], tail[maxn];

int main()
{
int T; scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
if (n == 1) {
printf("1\n"); continue;
}

head[n - 1] = tail[0] = 1;
for (int i = n - 2; i >= 0; i--) {
head[i] = (a[i] < a[i + 1]) ? head[i + 1] + 1 : 1;
}
for (int i = 1; i < n; i++) {
tail[i] = (a[i] > a[i - 1]) ? tail[i - 1] + 1 : 1;
}

for (int i = 0; i <= n; i++) f[i] = INF;
int ans = 0;
for (int i = 0; i < n; i++) {
int pos = lower_bound(f + 1, f + 1 + i + 1, a[i]) - (f + 1);
ans = max(ans, pos + head[i]);
f[tail[i]] = min(a[i], f[tail[i]]);
}
printf("%d\n", ans);
}
return 0;
}