用一个数组记下递增子序列长度为i时最小的len[i],不断更新len数组,最大的i即为最长递增子序列的长度
#include<cstdio>
#include<algorithm>
#define MAX 40010
using namespace std;
int a, T, n, len[MAX];
int* lower(int &val, int R) //二分找值,返回下标
{
int L = , mid;
while (L < R)
{
mid = R - (R - L + ) / ; //保证至少减少1
if (len[mid] < val) L = mid + ;//至少增加1
else R = mid;
}
return &len[R];
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
int i, l = ;
scanf("%d", &a);
len[] = a;
for (i = ; i < n; i++)
{
scanf("%d", &a);
if (a>=len[l]) len[++l] = a;
else *lower(a, l) = a;
// else *upper_bound(len,len+l+1,a) = a;
}
printf("%d\n", l + );
}
return ;
}