Programming Algorithm-code for the longest ascending subsequence problem (C)

Source: Internet
Author: User

Programming Algorithm-code for the longest ascending subsequence problem (C)
Code for the longest ascending subsequence (C)

 

 

Question: there is a series with a length of n. Request the length of the longest ascending subsequence in this series. The numbers of the longest ascending subsequence can be separated.

That is, the Longest ascending Subsequence (LIS, Longest Increasing Subsequence), for example, n = 5, a = {, 3, 1, 5}, result = 3 (, 5 ).

 

UseDynamic Planning (DP).

Method 1: The longest ascending subsequence before each number is obtained in sequence,Time complexity O (n ^ 2).

Method 2: Obtain the oldest sequence of the last element, update the array with smaller elements, and applyBinary SearchSearch element,Time complexity (nlogn).

 

Code:

 

/** Main. cpp ** Created on: 2014.7.20 * Author: Spike * // * eclipse cdt, gcc 4.8.1 */# include
 
  
/** Main. cpp ** Created on: 2014.7.20 * Author: spike * // * eclipse cdt, gcc 4.8.1 */# include
  
   
# Include
   
    
# Include
    
     
# Include using namespace std; class Program {static const int MAX_N = 100; const int INF = INT_MAX> 2; int n = 5; int a [MAX_N] = {4, 2, 3, 1, 5}; int dp [MAX_N]; public: void solve () {int res = 0; for (int I = 0; I
     
      Output:
      

 

 

result = 3


 

 

 

 

 

 

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.