Introduction to Algorithms exercise 15.4-5 15.4-6 find the longest monotone increment sequence __ algorithm in a sequence of n numbers

Source: Internet
Author: User

Introduction to Algorithms 15.4-5 please give an O (n^2) time algorithm to find the longest monotonically ascending sequence in a sequence of n numbers.

This topic is in the dynamic Planning section, where it is obvious that dynamic programming is used.

We know that the two important features of a problem that can be used in dynamic programming are the optimal substructure and overlapping sub problems. Here's an analysis of the problem:

For the sequence nums of an n element, the longest monotone increment sequence is SM (n) (m is the length of the ascending subsequence), then there are two conditions for SM:

1. If Nums[n] is larger than the last element of Sm-1, SM is made up of Sm-1 plus nums[n]

2, if Nums[n] is less than or equal to the last element of Sm-1, then SM is Sm-1

But this is a problem. Because if there are more than one sequence of Sm-1, we should look at each one with Nums[n], if nums[n] is smaller or equal than all Sm-1 tail elements, and there is a sequence of tail elements less than Sm-2 in the nums[n sequence, how should we choose, How much SM should be. Sm-1+nums[n], Sm-1, Sm-2+nums[n]

So the previous analysis was problematic, and in the same length, we preferred the sequence containing nums[n] as SM (n), because each newly added element could change the incremented subsequence of different lengths, so we needed to maintain each increment subsequence to get the best.

The optimal substructure here is that the maximum increment subsequence of n elements is one of the ascending subsequence of the previous n-1 element or plus nums[n], which naturally has many overlapping child problems.

The code is as follows:

Introduction to the/************************************************************************//* Algorithm 15.4-5 * Find the longest monotonically ascending sequence in a sequence of n numbers * Using dynamic programming idea, time complexity is O (n^2)//************************************************************************/#include <
Iostream> using namespace std;
void printsequence (int *b,int* nums,int last);
	int main () {int n=8;
	int nums[9]={0,1,7,8,9,2,3,4,5}; b stores the previous element ordinal of the current element in the incrementing sequence of the current element//c stores the ordinal length of the last element in the sequence that is the end of the current element//last the length of the longest incrementing sequence in the series that stores the current element//maxlen stores the lengths of the current maximum increment sequence int b
	[9]={0},c[9]={0},last[9]={0},maxlen=0;
	C[1]=1,last[1]=1; for (int i=1;i<=n;i++) {for (int j=1;j<i;j++) {if (Nums[j]<nums[i] && c[j]+1>c[i]) {C
				[I]=c[j]+1;
				B[i]=j;
				Last[i]=i;
			Maxlen=c[i];
				}else if (C[j]>c[i]) {maxlen=c[j];
			LAST[I]=LAST[J];
	}} cout<< "The original sequence length is" <<n<< ", as follows:" <<endl;
	for (int i=1;i<=n;i++) {cout<<nums[i]<< ""; cout<<endl<< "Maximum increment subsequence length is" <<maxLen<< ", as follows:" <<endl;
	Printsequence (B,nums,last[n]);
	cout<<endl;
return 0;
	} void printsequence (int *b,int* nums,int last) {if (b[last]>0) printsequence (b,nums,b[last)); cout<<nums[last]<< "";}


The time complexity of the above program is O (n^2), a total of n child problems, each child problem has n choices.
15.4-6 also requires the use of O (NLGN) time complexity to find the longest ascending subsequence, the specific algorithm thought can refer to:
http://blog.csdn.net/wdq347/article/details/8978394
I'll go directly to the code below.

Introduction to the/************************************************************************//* Algorithm 15.4-6 * Find the longest monotonically ascending sequence in a sequence of n numbers * Time Complexity of O (NLGN)//************************************************************************/#include <iostream
> #include <vector> using namespace std;
void printsequence (int *b,int* nums,int last);
	int main () {int nums[]={1,7,8,9,2,3,4,5};
	Array B stores the first element ordinal int n = sizeof (nums)/sizeof (int) of the current element in the incrementing sequence;
	int *b=new Int[n];
	The vector container last stores incrementing sequences of different lengths (subsequence of the same length, taking into account only the smallest sequence of tail elements)//The number of the final element in vector<int>;
	Last.push_back (0);
	b[0]=0;
		for (int i=1;i<n;i++) {int low=0,high=last.size ()-1;
		int middle= (Low+high)/2;
		int POS;
			if (Nums[i]>nums[last[high]]) {//If the current element is larger than all elements in last, the length of the subsequence is incremented by one, and the tail element is the current element nums[i] Last.push_back (i);
		B[i]=last[high];

				}else{//Otherwise, the minimum element while (Low 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.