最长递增部分

Code:
  1. #include <iostream>   
  2. #include <iomanip>   
  3. using namespace std;   
  4.   
  5. int longest_inc_sequence(int x[], int n)   
  6. {   
  7.  int *last;   
  8.  int length = 0;   
  9.  int left;   
  10.  int right;   
  11.  int mid;   
  12.  int i;   
  13.     
  14.  last = new int[n];   
  15.     
  16.  last[0] = x[0];   
  17.     
  18.  for (i = 1; i < n; ++i)   
  19.  {   
  20.   if (x[i] < last[0])   
  21.   {   
  22.    last[0] = x[i];   
  23.   }   
  24.   else if (x[i] > x[length])   
  25.   {   
  26.    ++length;   
  27.    x[length] = x[i];   
  28.   }   
  29.   else  
  30.   {   
  31.    left = 0;   
  32.    right = length;   
  33.    while (left <= right)   
  34.    {   
  35.     mid = (left + right) / 2;   
  36.     if (x[mid] > x[i])   
  37.     {   
  38.      right = mid - 1;   
  39.     }   
  40.     else  
  41.     {   
  42.      left = mid + 1;   
  43.     }   
  44.    }   
  45.    x[right] = x[i];   
  46.   }   
  47.  }   
  48.     
  49.  delete(last);    
  50.  return length + 1;   
  51. }   
  52.   
  53. int main(int argc, char *argv[])   
  54. {   
  55.   int  x[] = { 1, 3, 2, 1, 5, 7, 8, 6, 5, 9, 4, 10, 6 };   
  56.      int  n = sizeof(x)/sizeof(int);   
  57.      int  i;   
  58.   
  59.      cout << "/nLongest Increasing Sequence Program";   
  60.      cout << "/n===================================/n";   
  61.      cout << "/nGiven Array : ";   
  62.      for (i = 0; i < n; i++)   
  63.      {   
  64.           cout << setw(4) << x[i];   
  65.      }   
  66.      cout << "/n/nLength of L.I.S. is " << longest_inc_sequence(x, n) << endl;   
  67.  return 0;   
  68. }   
  69. /*  
  70. Longest Increasing Sequence Program  
  71. ===================================  
  72.  
  73. Given Array :    1   3   2   1   5   7   8   6   5   9   4  10   6  
  74.  
  75. Length of L.I.S. is 7  
  76. */  

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值