有一个长为n的数列a0,a1,…,a(n-1)。请求出这个序列中最长的上升子序列的长度。
上升子序列指的是对于任意的i<j都满足ai<aj的子序列。
1≤n≤1000
0≤ai≤1000000
输入
n=5
a={4,2,3,1,5}
输出
3(注:2,3,5)
#include<iostream>
#include<algorithm>
using namespace std;
int arr[]={1,2,4,8,754,43,34,56,7,343,34,324,34,23,434,34,323,234325,325345,43,3,433,4,2432,4,324324,23,4,35,34,3434,32,543,5,34,5,43,5,34,5,34534,5,324,3434,3,4,35,343,43,5,34,543,343,5,6,756,7,6,87,68,76,8,76,8,678,678,67,86,876888,6,5};
int dp[100];
int n=10;
int main()
{
int index=1;
dp[1]=arr[0];
for(int i=1;i<n;i++)
{
if(arr[i]>dp[index])
{
dp[index+1]=arr[i];
index++;
}
else
//二分nlogn
dp[upper_bound(dp,dp+index+1,arr[i])-dp]=arr[i];
//循环遍历n*n
// for(int j=0;j<=index;j++)
// if(dp[j]>arr[i])
// dp[j]=arr[i];
}
cout<<index;
}