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