You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
python/C++专题课程/动态规划/动态规划-4-最长上升子序列(2).cpp

33 lines
699 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}