Files
python/C++专题课程/动态规划/动态规划-4-最长上升子序列(2).cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

33 lines
731 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
/*
知识点内容:最长上升子序列2
文档内容参考:
https://www.cnblogs.com/aiguona/p/7278141.html
*/
#define MAXN 40005
using namespace std;
int arr[MAXN], ans[MAXN], len;
int main() {
int n;
int T;
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
ans[1] = arr[1];
len = 1;
for (int i = 2; i <= n; ++i) {
if (arr[i] > ans[len])
ans[++len] = arr[i];
else {
int pos = lower_bound(ans, ans + len, arr[i]) - ans;
ans[pos] = arr[i];
}
}
cout << len << endl;
}
return 0;
}