Introduction to dynamic planning (DP) ------- longest non-downsample sequence (O (n ^ 2), dp -------

Source: Internet
Author: User

Introduction to dynamic planning (DP) ------- longest non-downsample sequence (O (n ^ 2), dp -------

When I first wrote a blog, I shared the longest non-Downloading subsequence of the DP problem I just learned, that is, the dynamic planning problem.

Problem description: You can enter a number or a number to output the maximum length of the non-descending subsequence. For example, 5, 3, 4, 8, 6, and 7, the longest non-descending subsequence length is 4.

In the face of such a problem, we must first define a "state" to represent its subproblem and find its solution. Note: In most cases, a State is only related to the State that appears before it, but independent of the State that follows it (a Way of Thinking ).

Suppose we want to calculate A [1], A [2],…, The maximum length of the non-descending subsequence of A [I], where I <N, then the above problem becomes A subproblem of the original problem (The problem scale is reduced, you can let I = 1, 2, 3, etc. for analysis) Then we define d (I), which indicates the length of the longest non-descending subsequence ending with A [I] In the first I number. OK. You can estimate that d (I) is the status we are looking for against the simple question in "Getting started. If we calculate all the values from d (1) to d (N), the answer we are looking for is the largest one. The status is found, and the next step is to find the state transition equation.

  • The LIS length of the first number d (1) = 1 (sequence: 5)
  • The LIS length of the first two numbers d (2) = 1 (sequence: 3; 3 front is not smaller than 3)
  • The LIS length of the first three numbers d (3) = 2 (sequence: 3, 4; 4 there is a 3 smaller than it, So d (3) = d (2) + 1)
  • The length of LIS in the first four numbers d (4) = 3 (sequence: 3, 4, 8; 3 before 8 is smaller than it, So d (4) = max {d (1), d (2), d (3)} + 1 = 3)

 

I can find it.D (I) = max {1, d (j) + 1}, where j <I, A [j] <= A [I]

 

The following is the code (c ):

1 # include <stdio. h> 2 # define Max 10 3 int list (int A [], int n); 4 int list (int A [], int n) // compare the size relationships of multiple d [j], and compare each time I is determined (d [j] + 1) comparison with 1 and initialization issues with d [I] 5 {int * d = (int *) malloc (sizeof (int) * n); // create a dynamic array, d is the array name 6 int j, I; 7 int len = 1; // d [n] indicates the longest non-descending subsequence before the nth Element 8 for (I = 0; I <n; ++ I) // I is used to traverse each element in the array 9 {10 d [I] = 1; // each I has a definite longest non-descending subsequence, therefore, you need to initialize d [I] 11 for (j = 0; j <I; ++ j) each time. // I is determined to be a value, in this case, we need to traverse the numbers before comparing I and I (j) 12 {13 if (A [j] <= A [I] & d [j] + 1> d [I]) // before I (j) the largest d [I] is required for multiple I-hours, similar to (a> max, max = a) 14 d [I] = d [j] + 1; 15} 16 if (d [I]> len) len = d [I]; // len returns the maximum value 17} 18 free (d); 19 20 return len; 21} 22 int main (void) 23 24 {25 26 int A [Max]; 27 int I; 28 for (I = 0; I <Max; I ++) 29 {30 scanf ("% d", & A [I]); 31} 32 printf ("the maximum length of the array's non-descending subsequence is % d \ n ", list (A, 10); 33 return 0; 34}

 

 

Related Article

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.