nlogn的最长子序列算法代码

nlogn的最长子序列算法代码

文章

#include < iostream >
using   namespace  std;
int  n;
int  size;
int  c[ 10001 ];
int  f[ 10001 ];
int  a[ 10001 ];
int  bsearch( int  ai)
{
        
int  l = 1 ,r = size;
        
while (l <= r)
        {
                
int  mid = (l + r) >> 1 ;
                
if (ai > c[mid - 1 ] && ai <= c[mid])  return  mid;
                
else   if (ai > c[mid]) l = mid + 1 ;
                
else  r = mid - 1 ;
        }
}
int  main()
{
        scanf(
" %d " , & n);
        
for ( int  i = 1 ;i <= n; ++ i) scanf( " %d " , & a[i]);
        c[
1 ] = a[ 1 ]; f[ 1 ] = 1 ; size = 1 ;
        
for ( int  i = 2 ;i <= n; ++ i)
        {
                
int  j;
                
if (a[i] <= c[ 1 ]) j = 1 ;
                
else   if (a[i] > c[size]) j =++ size;
                
else  j = bsearch(a[i]);
                c[j]
= a[i]; f[i] = j;
        }
        printf(
" %d\n " ,size);
        
return   0 ;
}





你可能感兴趣的:(nlogn的最长子序列算法代码)