The longest non-descending subsequence Nlogn algorithm detailed

Source: Internet
Author: User

It took a long time today to finally understand the algorithm ... After all, to find a good explanation is really too difficult, so inspirational I want to write a good explanation qaq

This article is based on a n^2 solution that understands this problem.

Problem solved: Given a sequence, the length of the longest non-descending subsequence (Nlogn's algorithm cannot find out what the specific sequence is)

Definition: A[1..N] is the original sequence, D[k] represents the minimum value of the element at the end of a non-descending subsequence with a length of K, and Len represents the length of the currently known oldest sequence.

Initialize: d[1]=a[1]; Len=1; (0 elements for a special sentence)

Now we know that the longest non-descending subsequence length is 1, the minimum value of the end element is a[1], then we let I from 2 to n loop, in order to find the length of the longest non-descending sub-sequence of the first I element, we only need to maintain a good d this array and Len can be.

The key question is how to maintain?

It can be seen that we are going to use the complexity of LOGN maintenance. Actually utilizes a property of the D array: monotonicity. (Longer, d[k] value is not reduced)

consider the new coming in an element A[i]:

If this element is greater than or equal to D[len], directly let D[len+1]=a[i], and then len++. This is a good understanding, the longest length of the current becomes len+1, and the D array adds an element.

What if this element is less than D[len]? It means it can't be followed by the last one. So let's see who it's supposed to be after.

To be exact, it's not behind who. Instead of replacing who. Because it is in front of who is meaningless, and then the longest Len, so is to replace others. So who's to replace it? is to replace the one that is most replaced by it. That is, the first one in the D array is greater than it. The first one means that the front is less than equal to it. Assuming that the first is greater than it is d[j], stating that d[1..j-1] is less than or equal to it, then it can be completely connected to d[j-1] and then generate a non-descending subsequence of length J, and this subsequence has more potential than the current D[J] sub-sequence (because this number is smaller than d[j]) . So just replace it, that's d[j]=a[i]. In fact, this position is also the only place where it can be replaced (the previous substitution for the definition of the d[k] minimum value, followed by a non-satisfied non-descending sequence)

As for the first one greater than it how to find ... STL upper_bound. Each time the complexity of LOGN.

So far, we have magically solved this problem. According to this idea, what if you need to require a strictly incrementing subsequence?

still consider the new coming in an element A[i]:

If this element is greater than D[len], directly let D[len+1]=a[i], and then len++. This is a good understanding, the longest length of the current becomes len+1, and the D array adds an element.

What if this element is less than or equal to D[len]? It means it can't be followed by the last one. So let's see who it's supposed to be after.

To be exact, it's not behind who. Instead of replacing who. Because it is in front of who is meaningless, and then the longest Len, so is to replace others. So who's to replace it? is to replace the one that is most replaced by it. That is, the first one in the D array is greater than or equal to it. The first one means that the front is smaller than it. Assuming that the first is greater than or equal to it is d[j], the description d[1..j-1] is less than it, then it can be connected to d[j-1] and then generate a J-length ascending subsequence, and this sub-sequence than the current D[J] This sub-sequence more potential (because this number is smaller than d[j]) . So just replace it, that's d[j]=a[i]. In fact, this position is also the only place where it can be replaced (the previous substitution for the definition of d[k] minimum, followed by a non-satisfied ascending sequence)

As for the first greater than equals it how to find ... STL lower_bound. Each time the complexity of LOGN.

The following code is the longest non-descending subsequence

//Longest non-descending subsequence nlogn Song#include<cstdio>#include<algorithm>using namespacestd;inta[40005];intd[40005];intMain () {intN; scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]); if(n==0)//0 Elements for a special sentence .{printf ("0\n"); return 0; } d[1]=a[1];//Initialize    intlen=1;  for(intI=2; i<=n;i++)    {        if(A[i]>=d[len]) d[++len]=a[i];//If you can pick it up after Len.        Else  //Otherwise, find a replacement that is the most replacement.        {            intJ=upper_bound (d+1, d+len+1, A[i])-D;//Find the first subscript that is larger than its dd[j]=A[i]; }} printf ("%d\n", Len); return 0;}

After a night of thinking, the problem finally figured out.

The longest non-descending subsequence Nlogn algorithm detailed

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.