典型的lis水题一道。重点要掌握lis和lds的0(nlogn)算法,直接贴代码:160K+16MS
#include <stdio.h>
#include <stdlib.h>
#define Max 1010
#define Inf 1000010
int a[Max];
int c[Max];
int n;
int Binary_search(int value){
int left=0,right=n+1,mid;
while(left<=right){
mid=(left+right)>>1;
if(c[mid]<value)
left=mid+1;
else if(c[mid]>value)
right=mid-1;
else
return mid;
}
return left;
}
int lis(){
c[0]=-Inf;
c[1]=a[1];
int i,j,result=1;
for(i=2;i<=n;i++)
c[i]=Inf;
for(i=2;i<=n;i++){
j=Binary_search(a[i]);
c[j]=a[i];
if(j>result)
result=j;
}
return result;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("%d\n",lis());
return 0;
}