POJ 2533 Longest Ordered Subsequence 编程之美 2.16 求数组中最长递增子序列

当考察第i+1个元素的时候,不考虑前面i个元素的分布情况:

#include <iostream>
using namespace std;

int LongestOrderedSubsequence(int arr[], int n);

int main()
{
	int n;
	int *arr;

	cin>>n;

	arr = new int [n];
	
	cout<<LongestOrderedSubsequence(arr, n)<<endl;

	// system("pause");
	return 0;
}


int LongestOrderedSubsequence(int arr[], int n)
{
	unsigned int max = 1;
	int i, j;
	int *len;
	len = new int[n];

	for (i=0; i<n; i++)
	{
		cin>>arr[i];
		len[i] = 1;
	}

	for (i=1; i<n; i++)
	{
		for (j=i-1; j>=0; j--)
		{
			if (arr[i] > arr[j] && len[i] < (len[j] + 1))
			{
				len[i] = len[j] + 1;
				if (len[i] > max)
				{
					max = len[i];
					break;
				}
			}
		}
	}

	return max;
}

待续

你可能感兴趣的:(POJ 2533 Longest Ordered Subsequence 编程之美 2.16 求数组中最长递增子序列)