找出数组A[1...n]的最大不降子序列。
public class Max_Increase_Order {
public static void main(String args[]){
int a[]={1,3,2,5,4,9};
maxIncOrder(a);
}
public static int query(int a[],int num,int goal){ //返回x,则a[x-1]<goal<=a[x]
int left=0,right=num,mid=(left+right)/2;
while(left<=right){
if(a[mid]<goal) left=mid+1;
else if(a[mid]>goal) right=mid-1;
else return mid;
mid=(left+right)/2;
}
return left;
}
public static void fill(int c[],int n){
for(int i=0;i<=n;i++){
c[i]=1000;
}
}
public static void maxIncOrder(int s[]){
int i,j,k,max;
int b[]=new int[100];
int c[]=new int[100]; //c[2]=3表达长度为2的最大递增子序列末尾最小值为3
fill(c,s.length+1);
c[0]=-1;
c[1]=s[0];
b[0]=1;
for(i=1;i<s.length;i++){
j=query(c,s.length,s[i]);
c[j]=s[i];
b[i]=j;
}
for(max=k=0;k<s.length;k++){
if(b[k]>max){
max=b[k];
}
}
for(int num=1;num<=max;num++){
System.out.print(c[num]+" ");
}
}
}