这个题和HDU1025异曲同工,一个意思,就是求最长不降子序列。
代码:
#include<stdio.h>
#include<string.h>
int BinSearch(int key,int* d,int left,int right)
{
int mid;
while(left<=right)
{
mid=(left+right)/2;
if(key>d[mid])
{
if(key<=d[mid+1]) return mid;
else left=mid+1;
}
else right=mid-1;
}
return 0;
}
int main()
{
int n;
int a[40001],d[40001];
int i,j,len;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
memset(d,0,sizeof(d));
for(i=0;i<n;i++)
scanf("%d",&a[i]);
len=1;
d[len]=a[0];
for(i=1;i<n;i++)
{
if(a[i]>d[len])
{
len++;
j=len;
}
else
{
j=BinSearch(a[i],d,1,len);
j++;
}
d[j]=a[i];
}
printf("%d\n",len);
}
return 0;
}