Recently Lou was boyfriend with play DotA, a little addicted, finally made a big decision last night, shift+delete delete wariii folder, from the DotA infighting. But recently saw the male ticket has turned the battlefield from 11 to the TopCoder, well, this is a good phenomenon, hoped that the building also can follow the play up.
Ideal is beautiful, alas, but the landlord is still in the early stage of the beauty of programming ah. Words do not say much, I hope that their refueling refueling and refueling!! (^o^)/~
One of the topics to look at today is to ask for the longest increment subsequence in the array.
Topic Description:
Write a program with the lowest possible time complexity, and find the length of the longest increment subsequence in a one-dimensional array (N) element. For example: In sequence 1,-1, 2,-3, 4,-5, 6,-7, the longest increment subsequence has a length of 4, this subsequence is 1, 2, 4, 6.
Note: The title only asks for length, not necessarily the increment sequence.
Solution Analysis:
Here, to remember a particularly important point (for like the landlord of this slag), that is to meet the no- effect can be used to solve the dynamic recursion. What is called no- effect , such as 1,-1, 2,-3, 4, 5, 6, 7, when we find -1,2, when we traverse to element 4, we only need to compare the relationship between 4 and 2, if 4:2 is large, then you can add in. The elements before 2 have no effect on our decision, which is called "No validity ". So our solution is:
for any one increment subsequence of the preceding I element, if the maximum element of the subsequence is smaller than array[i+1], then the array[i+1] element can be added to the subsequence, forming a new increment subsequence.
For details, see the Code, Algorithm complexity O (n^2), the code I have a very detailed explanation, what do not understand, please leave a message.
#include <iostream>#include <vector>using namespace STD;intLIS (int*parray,intLen);intGetminofarray (int*parray,intLen);intMain () {intA[] = {2, -1,2, -3,4,3,1,2, -7,3,4,5,6,7,8,9};intLen =sizeof(a)/sizeof(int); vector<int>Longestsubsequence;cout<<"The length of the maximum increment sequence is"<< LIS (A, Len) << Endl; System"Pause");}intLIS (int*parray,intLen) {int*MAXV =New int[Len +1](); maxv[1] = parray[0]; maxv[0] = Getminofarray (Parray, Len)-1;int*lis =New int[Len] ();//Initialize information for maximum increment sequence for(inti =0; i < Len; i++) {Lis[i] =1; }intNmaxlis =1; for(inti =0; i < Len; i++) {intJ//The code below is recursive to see if there is a maximum value for the largest element of the longest increment sequence, and if so, it can be added directly to the back. Then the number of the longest increment sequence plus 1 //For example, when i = 4, Nmaxlis = 2, maxv[2] = 2, a[i] = 4, 4 > 2, then 4 can be added after 2, incrementing the number of elements of the sequence plus 1. Once found, break directly. for(j = nmaxlis; J >=0; j--) {if(Parray[i] > Maxv[j]) {Lis[i] = j +1; Break; } }//If the maximum increment sequence at I is larger than the original Nmax, then it is updated. and the minimum value of the largest element of the longest increment sequence is updated to the current element if(Lis[i] > Nmaxlis) {nmaxlis = lis[i]; Maxv[lis[i]] = parray[i]; }//If it is not longer than the original maximum length, then it is possible to update the minimum value of the largest element of this length. For example, when Nmaxlis = 3, the sequence we found earlier is {1, 2, 4} or {-1, 2, 4} //When i = 5 o'clock, j = 2 o'clock jumps out of the loop. LIS[5] = 3 <= nmaxlis, at that time 3:4 small, so nmaxlis = 3 o'clock the minimum value of the maximum element is updated from 4 to 3. Else if(Maxv[j] < Parray[i] && Parray[i] < Maxv[j +1]) {maxv[j +1] = Parray[i]; } }Delete[]MAXV;Delete[]lis;returnNmaxlis;}//Gets the minimum value of the array elementintGetminofarray (int*parray,intLen) {intresult = parray[0]; for(inti =0; i < Len; i++) {if(Parray[i] < result) {result = Parray[i]; } }returnResult;}
The beauty of programming 5: Finding the longest increment subsequence in an array