Poj2533 longest ordered subsequence

Source: Internet
Author: User

Question:
Pure longest ascending subsequence (LIS: longest increasing subsequence)
State transition equation (time complexity is O (n * n ))
If (A [I] <A [J])
DP [I] = max (DP [I], DP [J] + 1 );

Solution 1:
Based on the state transition equation:
If (A [I] <A [J])
DP [I] = max (DP [I], DP [J] + 1 );

# Include <iostream>
# Include <cstring>
Using namespace STD;
# Define x 1003
Int DP [x];
Int A [x];
Int max (int A, int B)
{
Return A> B? A: B;
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int N;

While (CIN> N)
{
Memset (A, 0, sizeof ());
For (INT I = 1; I <= N; I ++)
Cin> A [I];
For (INT I = N; I> = 1; I --)
For (Int J = I + 1; j <= N; j ++)
If (A [I] <A [J])
DP [I] = max (DP [I], DP [J] + 1); // CoreAlgorithm
Int max = 0;
For (INT I = 0; I <= N; I ++) // find the largest ascending subsequence
If (max <DP [I])
Max = DP [I];
Cout <MAX + 1 <Endl;
}
Return 0;
}

Solution 2:
O (N ^ 2) algorithm analysis is as follows: (Num [1]... num [N] stores all input numbers)
1. For num [N], because it is the last number, when you start to search for num [N], there is only a child sequence with a length of 1 and no descent;
2. If you start searching for num [n-1], there are two possibilities:
(1) If num [n-1] <num [N], there will be a child sequence num [n-1], num [N] with a length of 2.
(2) If num [n-1]> num [N], there will be a child sequence num [n-1] Or num [N] with a length of 1.
3. Generally, if the subsequence starts from num [J], the maximum length of the subsequence is not decreased at this time, which should be obtained using the following method:
In a [J + 1], a [J + 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. For algorithm requirements, two arrays are defined: num [Max] Memory number Lis [Max] Maximum ascending sub-sequence length starting from I

You can also:
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.

# Include <iostream>
# Include <cstring>
Using namespace STD;
# Define x 1003
Int DP [x];
Int A [x];
Int max (int A, int B)
{
Return A> B? A: B;
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int N;

While (CIN> N)
{
Memset (A, 0, sizeof ());
For (INT I = 1; I <= N; I ++)
Cin> A [I];
Int ans = 0;
For (INT I = N; I> = 1; I --)
{
Int max = 0;
For (Int J = I + 1; j <= N; j ++)
If (A [I] <A [J] & DP [J]> MAX)
Max = DP [J];
DP [I] = MAX + 1;
If (DP [I]> ans)
Ans = DP [I];
}
Cout <ans <Endl;
}

Return 0;
}

 

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.