Algorithm: The longest ascending descending subsequence and the longest descending sequence

Source: Internet
Author: User

Algorithm: The longest ascending descending subsequence and the longest descending sequence
Problem

Given the number of n, take the number of x (x> = 0) from it, so that the remaining number has the following properties.
A1 <A2 <A3 <... At> At + 1> At + 2>... >
The number of records to be extracted is at least a few.


Illustration

Code

#include "stdafx.h"const int MAX=105;int down[MAX],up[MAX];int h[MAX],n;void get_up(){int i,tmp,j;for(i=0;i<n;i++){tmp=1;for(j=i-1;j>=0;j--){if(h[i]>h[j]&&up[j]+1>tmp)tmp=up[j]+1;}up[i]=tmp;}}void get_down(){int i,j,tmp;for(i=n-1;i>=0;i--){tmp=1;for(j=i+1;j<n;j++){if(h[i]>h[j]&&down[j]+1>tmp)tmp=down[j]+1;}down[i]=tmp;}}int main(){int i,max;while(scanf("%d",&n)!=EOF){for(i=0;i<n;i++)scanf("%d",&h[i]);get_up();get_down();max=0;for(i=0;i<n;i++){if(up[i]+down[i]-1>max)max=up[i]+down[i]-1;}printf("%d\n",n-max);}return 0;}




How to write the nlogn algorithm of the longest ascending subsequence?

A: array [0 .. 100001] of int64; // array
D: array [0 .. 100001] of int64; // queue
Now: longint;
Function search (x: longint): longint; // returns the position of the closest value to X.
Var l, r, mid: longint;
Begin
L: = 1; r: = top;
While l <> r do
Begin
Mid: = (l + r) div 2;
If d [mid]> x then r: = mid else l: = mid + 1;
End;
Exit (l );
End;
Begin
Fillchar (d, sizeof (d), 0); // Initialization
Top: = 1; // top indicates the number of generated queues.
Now: = 1;
D [1]: = a [1];
For I: = 2 to n do
Begin
If a [I] <= d [1] then j: = 1;
If a [I]> = d [top] then j: = top + 1;
If (a [I]> d [1]) and (a [I] <d [top]) then
J: = search (a [I]);
Now: = j;
If j> top then begin inc (top); d [top]: = a [I]; end;
If a [I] <d [j] then d [j]: = a [I];
If now> max then max: = now; // now is the maximum value.
End;
// The specific idea is:
Because D is monotonous, I can process the number in the array below!
I want to generate a longest ascending subsequence, put it in array D, from 1 to N each enumeration of each number,
Perform the following operations on it:
1: if it is smaller than D [TOP], it indicates that it is currently the smallest, j: = 1;
2: If it is larger than D [TOP], add it to the queue. j: = top + 1;
3: If he is smaller than D [top] and greater than D [1], the second part searches for his position in D queue. j: = search (a [I]);
This is the specific idea!
What else don't you know ??

Interpretation of the longest rising sequence dynamic planning Equation

There are two algorithms: O (n * logn) and O (n ^ 2)

O (n ^ 2) algorithm analysis is as follows: (a [1]... a [n] stores all input numbers)
1. For a [n], because it is the last number, when searching from a [n], there is only a child sequence with a length of 1 and no descent;
2. If you start searching from a [n-1], there are two possibilities:
(1) If a [n-1] <a [n], there is a child sequence a [n-1], a [n] with a length of 2.
(2) If a [n-1]> a [n], there is a child sequence a [n-1] or a [n] with a length of 1.
3. Generally, if the subsequence starts from a [t], the maximum length of the subsequence is not decreased at this time, which should be obtained using the following method:
In a [t + 1], a [t + 2],... in a [n], find a child sequence that is larger than a [t] and is the longest and does not drop as its successor.
4. Define an array for algorithm needs:
D: array [1. n, 1. 3] of integer;
D [t, 1] indicates a [t]
D [t, 2] indicates the maximum length of the subsequence from the I position to n.
D [t, 3] indicates the next position of the subsequence starting from position I.
The analysis of the O (n * logn) algorithm of the longest sub-sequence without descent is as follows:
First, review the classical O (n ^ 2) dynamic planning algorithm, and set A [t] to represent the number of t in the sequence, F [t] indicates the length of the longest ascending subsequence ending with t from 1 to t. It is set to F [t] = 0 (t = 1, 2 ,..., len ()). Then there is a dynamic planning equation: F [t] = max {1, F [j] + 1} (j = 1, 2 ,..., t-1, and A [j] <A [t]).
Now, we carefully consider the situation when calculating F [t. Assume that two elements A [x] and A [y] Meet
(1) x <y <t (2) A [x] <A [y] <A [t] (3) F [x] = F [y]
Select F [x] and select F [y] to obtain the same F [t] value. Then, in the position of the longest ascending subsequence, should I select A [x] or A [y?
Obviously, selecting A [x] is better than selecting A [y. Because of the condition (2), in A [x + 1]... in the section A [T-1], if A [z], A [x] <A [z] <a [y, A longer ascending subsequence is obtained.
Then, based on the condition (3), we will get an inspiration: classification based on the value of F. For each value k of F [], we only need to retain the minimum values of all A [t] that satisfy F [t] = k. Set D [k] to record this value, that is, D [k] = min {A [t]} (F [t] = k ).
Note the two features of D:
(1) The value of D [k] does not increase monotonically throughout the calculation process.
(2) The values of D [] are ordered, that is, D [1] <D [2] <D [3] <... <D [n].
Using D [], we can obtain another method to calculate the longest ascending subsequence length. Set the maximum length of the obtained ascending subsequence to len. First, judge A [t] And D [len]. If A [t]> D [len], A [t] is connected to D [len] and A longer ascending subsequence is obtained. len = len + 1, D [len] = A [t]; otherwise, in D [1] .. in D [len], find the largest j, satisfying D [j] <A [t]. If k = j + 1, then D [j] <A [t] <= D [k], after A [t] is connected to D [j], A longer ascending subsequence is obtained, and D [k] = A [t] is updated. Finally, len is the length of the requested longest ascending subsequence.
In the above algorithm, if you use the simple sequence search in D [1]... D [len], the full text is left by...>

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.