时间复杂度为O(nlogn)的最长单调递增子序列

写一记,其中利用二分查找法,具体分析见编程之美。

#include 
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<


 

你可能感兴趣的:(数据结构与算法)