常规方法:(n^2)
#include<iostream>
using namespace std;
int i,j,n,a[100],b[100],max;
int main()
{
cin>>n;
for(i=0;i<n;i++) cin>>a[i];
b[0]=1; //初始化,以a[0]结尾的最长递增子序列长度为1
for(i=1;i<n;i++)
{
b[i]=1;//b[i]最小值为1
for(j=0;j<i;j++)
if(a[i]>a[j]) b[i]=max(b[i],b[j]+1);
}
for(max=i=0;i<n;i++) if(b[i]>max) max=b[i];
cout<<max<<endl;
}
优化之后:(n log(n))
#include <iostream>
using namespace std;
int i,j,n,s,t,a[100001];
int main()
{
cin>>n;
a[0]=-1000000;
for(i=0;i<n;i++)
{
cin>>t;
if(t>a[s]) a[++s]=t;
else
{
int l=1,h=s,m;
while(l<=h)
{
m=(l+h)/2;
if(t>a[m]) l=m+1;
else h=m-1;
}
a[l]=t;
}
}
cout<<s<<endl;
}