The beauty of programming Reading Notes 17: 2.16 find the longest incrementing subsequence in the array

Source: Internet
Author: User

The beauty of programming Reading Notes 17: 2.16 find the longest incrementing subsequence in the array

 

Idea: after each processing of a number, you can insert this number into an incremental subsequence that has been found (assuming it contains an infinite number of null sequences with a length of 0, increase the length by 1. After processing, the maximum length is required.

 

An incremental subsequence with the same length I + 1. If the minimum value of the last number of these sequences is min_v [I], the sequence is, if a number can be inserted after sequence a, it must be inserted after other incremental subsequences with the same length. However, whether it can be inserted after sequence a is determined only by the value of min_v [I, therefore, it is enough to maintain the min_v [I] value.

 

The current number must be inserted into a certain length of J (0 <= j <= Len,LenAfter the sequence is found to be the longest incrementing sub-sequence length), the old min_v [J + 1] (assuming that each value of the array is initialized to an infinite number) is equal to this number, min_v [J + 1] must be updated to this value. If the sequence is strictly incrementing, only one such J value can be found. If the sequence is not strictly incrementing, more than one J value may be found, and the maximum value of all J values can be obtained, the value of min_v [J + 1] is not changed because it is inserted to other products.

 

Specific process:

Use min_v [I] To save the minimum value of the last number of incremental subsequences with all currently found lengths of I.

Use Len to save the length of the currently found longest incremental sub-sequence.

For the input SRC [N], it is clear that min_v [0] = SRC [0], then Len = 1

Starting from the second number of input arrays (assuming that the subsequence is strictly incrementing)

Compare the last value of the subsequence with the current value and length Len (min_v [len-1])

1IfValue> min_v [len-1]Value can be added after the longest ascending sub-sequence found to obtain a longer ascending sub-sequence with the length of Len + 1, that is, min_v [Len] = value, and the Len value is added to 1.

2IfValue <= min_v [len-1], You can find 0 <= j <= len-1 satisfied: value <= min_v [J],

If J = 0, value can form a sub-sequence with a length of 1,

If j> 0, the value can be added to the subsequence whose length is J determined by min_v [J-1] to obtain a new subsequence whose length is J + 1.

All have min_v [J] = Value

 

If you want to output the longest incrementing subsequence, you can use an array info [I] to record the I + 1 SRC [N] to obtain the length of the longest incremental sub-sequence (info [I] is equal to the position where the value to be written is min_v add 1 ). Scan the two arrays at the same time. From the back to the front, first find the first number A with the corresponding length of Len, then find the first number whose length is len-1 and smaller than a, and so on all the numbers.

 


Size_t Lis ( Const Int SRC [], size_t sz)
{
If (SRC = Null | SZ < 1 ) Return 0 ;
// Min_v [I] is the minimum value of the last value of all incremental subsequences whose length is I + 1.
Vector < Int > Min_v (sz );
Min_v [ 0 ] = SRC [ 0 ];
// Len is the length of the longest incrementing subsequence currently found.
Size_t I, Len = 1 ;
Int Value;
For (I = 1 ; I < SZ; ++ I) {
Value = SRC [I];
// Suppose that the ascending subsequence is strictly incrementing.
If (Value > Min_v [Len - 1 ]) Min_v [Len ++ ] = Value;
Else * Lower_bound ( & Min_v [ 0 ], & Min_v [Len], value) = Value;
}
Return Len;
}

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.