Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public static int search(int[] M, int[] A, int i, int L ) {
  2.     int j = 0;
  3.     int k = L-1;
  4.     while( j <= k ) {
  5.         int m = ( j + k ) / 2;
  6.         if( A[M[m]] <= A[i] ) j = m + 1;
  7.         else k = m - 1;
  8.     }
  9.    
  10.     return k;
  11. }
  12.  
  13. public static int[] findLIS(int[] A) {
  14.     int n = A.length;
  15.     int[] M = new int[n];
  16.     int[] P = new int[n];
  17.     M[0] = 0;
  18.     P[0] = -1;
  19.     int L = 1;
  20.    
  21.     for(int i=1; i<n; ++i) {
  22.         int j = search( M, A, i, L );
  23.         if( j == -1 ) P[i] = -1;
  24.         else P[i] = M[j];
  25.        
  26.         if( j == L-1 || A[i] < A[M[j+1]] ) {
  27.             M[j+1] = i;
  28.             if( j+2 > L ) L = j+2;
  29.         }
  30.     }
  31.    
  32.     int[] LIS = new int[L];
  33.     n = L-1;
  34.     int p = M[n];
  35.     while( n >= 0 ) {
  36.         LIS[n] = A[p];
  37.         p = P[p];
  38.         n--;
  39.     }
  40.    
  41.     return LIS;
  42. }