这个题和最长上升子序列都是一个类型的题
题意是求一个序列的最长上升子序列的长度
输入数据是:
2
8
100 287 155 300 299 178 158 65
3
88 23 65
输出是:
5
2
是序列dp的入门题
代码:
#include <iostream> #include <cstring> using namespace std; int dp[1010]; int nums[1010]; int main(){ int n; cin>>n; while(n--){ int m; cin>>m; for(int i=1;i<=m;i++){ cin>>nums[i]; } int res=0; for(int i=1;i<=m;i++){ dp[i]=1; for(int j=1;j<i;j++){ if(nums[i]<nums[j]){ dp[i]=max(dp[i],dp[j]+1); } } res=max(res,dp[i]); } cout<<res<<endl; } }
这个题好像有个二分的做法可以把时间复杂度降到nlog(n)的时间复杂度(有时间写一下)