写一记,其中利用二分查找法,具体分析见编程之美。
#include <iostream>
using namespace std;
#define N 20
int binarySearch(int src[], int des, int low, int high)
{
int i = low, j = high;
while (low < high)
{
int mid = (low + high) / 2;
if (des > src[mid])
{
low = mid+1;
}
else if (des < src[mid])
{
high = mid;
}
else
return mid;
}
return low;
}
int LIS_Length(int X[], int len)
{
int LIS[N];
LIS[0] = INT_MIN;
for (int i = 1; i < N; i++)
{
LIS[i] = INT_MAX;
}
for (int i = 0; i < len; i++)
{
int loc;
loc = binarySearch(LIS, X[i], 0, len);
LIS[loc] = X[i];
}
int count = 0;
for (int i = 1; i <= len; i++)
{
if (LIS[i] < INT_MAX)
{
cout<<LIS[i]<<" ";
count++;
}
}
cout<<endl;
return count;
}
int main()
{
int X[] = {3, 2, 5, 6, 1, 4, 7};
int len = 7;
cout<<LIS_Length(X, len);
}