Longest monotonic decreasing subsequence, monotonic decreasing subsequence

Source: Internet
Author: User

Longest monotonic decreasing subsequence, monotonic decreasing subsequence

Problem description: Find the longest descending subsequence of an array, for example, {9, 4, 3, 2}. The longest descending subsequence is {9, 5, 4, 3, 2 }.

Idea: This is a standard dynamic planning problem. When you do not understand the algorithm, you are most likely to use the recursive idea. Actually, it is correct. At last, we will provide a recursive method, after knowing that it is a dynamic planning problem, we need to analyze it. We need an auxiliary array to record information. If the source array is src and the auxiliary array is table, the table [I] Array records src [0] ~ The src [I] sub-array contains the length of the longest monotonous sub-sequence consisting of src [I] (Note that table [I] records the decrease composed of src [I ]. the longest in the sequence, this is the longest descent sub-sequence ending with src [I]). If src [I] is greater than all the previous elements, then table [I] = 1.

The final result is: table [I] = max {table [k] + 1, src [I]> src [k] & 0 <k <I }, this is the table array initialized, so the longest descending subsequence is max {table [I], 0 <I <n }.

However, we need to print the oldest sequence. We have recorded the subscript of max {table [I], 0 <I <n}. Then we know the last element of the oldest sequence, at the same time, we know the length of the eldest son sequence. The following is a recursive call. If we already know that table [k] is the largest, src [k] is the last in the longest descending subsequence, if table [I] + 1 = table [k] & src [I] <table [k], you need to promise src [I].

# Include <stdio. h> # include <stdlib. h> void Find (int src [], int n, int table []) {table [0] = 1; int I, j, maxi = 0; for (I = 0; I <n; I ++) {table [I] = 1; for (j = 0; j <I; j ++) {/* this may be a common error. if (src [j]> src [I]) {table [I] = table [j] + 1; if (src [j]> src [I]) table [I] = table [j] + 1; if (table [I]> table [maxi]) maxi = I;} */if (src [j]> src [I] & table [j] + 1> table [I]) {table [I] = table [j] + 1; // This is used to print the existence. The last array element of the maxi record is if (table [I]> Table [maxi]) maxi = I ;}}// printf ("% d \ r \ n", maxi) ;}void print (int src [], int table [], int maxi) {int I = maxi-1; for (; I> = 0; I --) {if (table [maxi] = table [I] + 1 & src [I]> src [maxi]) {print (src, table, I ); break ;}} printf ("% d", src [maxi]);} void FindLongestDSCArray2 (int * arr, int n) {int mark [9]; int link [9]; int I = 0; for (I = 0; I <n; I ++) {mark [I] = 0; link [I] =-1;} // link [0] = 1; int j = 0, maxMark = 0; for (I = 0; I <n; I ++) {MaxMark = 0; for (j = 0; j <I; j ++) {if (arr [j]> arr [I]) {if (maxMark <mark [j]) {maxMark = mark [j]; link [I] = j ;}} mark [I] = maxMark + 1 ;} for (I = 0; I <9; I ++) printf ("% d", mark [I]); printf ("\ r \ n "); for (I = 0; I <9; I ++) printf ("% d", link [I]); printf ("\ r \ n "); /* // Print () int node = 0; maxMark = 0; for (I = 0; I <n; I ++) {if (mark [I]> maxMark) node = I;} while (node! =-1) {printf ("% d", arr [node]); node = link [node];} */} int main () {int src [] = {9, 4, 3, 2, 5, 4, 3, 2, 4}; int table [9]; Find (src, 9, table); FindLongestDSCArray2 (src, 9 ); int I = 0; for (; I <9; I ++) printf ("% d", table [I]); printf ("\ r \ n "); return 0 ;}







Partial Order

Two Theorems of the ordinal set:
Theorem 1> when (X, ≤) is a finite ordinal set and r is the maximum chain size, X can be divided into r anti-chains that cannot be less.
Its Dual theorem is called the Dilworth theorem:
Theorem 2> when (X, ≤) is a finite ordinal set and m is the maximum size of the anti-chain, X can be divided into m chains but cannot be less.

Let's talk about binary. Three Yuan gives you space for thinking.
According to Theorem 2, the anti-chain of (X, Y, <=) is X1 <X2 & Y1> Y2 or X1> X2 & Y1 <Y2, Which is symmetric, you can find any result. if it is X1 <X2 & Y1> Y2, it is used to find the oldest sequence of point {X, Y} increasing monotonically according to x, and y decreasing monotonically.
For more information about how to find the longest monotonic decreasing subsequence (increasing), see my blog (references ).
Reference: hi.baidu.com/...9.html

Design an O (n2) time algorithm to find the longest monotonic increasing subsequence of the series composed of n numbers

Just use the greedy algorithm.

Create a stack and scan the entire sequence sequentially.
The first element is first placed in the stack,
For each element scanned later, if it is larger than the top element of the stack, it will be pushed into the stack. If it is smaller than the top element of the stack, it will be played one by one until it is found that the top element of the stack is smaller than it.
Record the stack length at a time and its maximum value. After a sequence scan is completed, the maximum length of the stack is the longest sequence, and the element in the stack at the maximum length is the largest incremental sequence.
This algorithm is O (N ^ 2) level, but faster than dp.
 

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.