O (n^2) solution and greedy O (NLGN) solution for the dynamic programming of the longest ascending subsequence _delete

Source: Internet
Author: User

Topic Description:

To give you a sequence, one action is to move a number to the other position in the sequence, and then ask at least a few operations to make the sequence order. For example, the sequence 7,1,3,2,6,5 only needs to move three times, move 3 to 2, move 5 to 6, and then move 7 to the last side.

Problem Equivalence conversion:

is actually one of the longest ascending subsequence problems. Because the whole process is essentially equivalent to the number of moves to be removed first, and then put back to the appropriate position. This requires that the number of the remaining digits be ordered after the number to be moved is removed. If you want to move the number of the less the better, that is tantamount to the remaining number of fixed the more the better.

The longest ascending subsequence problem:

A sequence X[1..N with n number is given to find its longest monotone ascending subsequence. The largest m and A1,
A2......,am, making a1<a2<......<am and X[a1]<x[a2]<......<x[am].

Analysis of Dynamic Programming Solutions: (O (n^2))

The classical O (n^2) dynamic programming algorithm, set A[i] represents the number of I in the sequence, F[i] represents the length of the longest ascending sequence ending with I in the section 1 through I, initially set f[i] = 0 (i = 1, 2, ..., Len (A)). There are dynamic programming equations: f[i] = max{1, f[j] + 1} (j = 1, 2, ..., i-1, a[j] < a[i)).

Greedy + Two-point search: (O (NLOGN))
Open a stack, each take the top of the stack element s and read the element A to compare, if a>s, then add the stack; if a<s, then the binary find the stack in the 1th number of larger than a, and replace. The last sequence length is the length of the stack.
It is also well understood that for X and Y, if X<y and E[y]<e[x], replace E[y with E[x], the longest sequence length does not change but the ' potential ' of the sequence Q increases.
Example: The original sequence is 1,5,8,3,6,7
Stack for 1,5,8, at this time read 3, then use 3 to replace 5, get the elements in the stack for 1,3,8, read 6, replace 6 with 8, get 1,3,6, read 7, get the final stack for 1,3,6,7, the longest increment subsequence for length 4.

Code:

#include <iostream>
using namespace Std;

Dp:dp[i] = max{1,dp[j]+1} (j=1,2, ..., i-1, and A[j]<a[i)).
Template <typename t>
int LIS_DP (T seq[],int N)
{
int *dp=new Int[n];
memset (dp,0,n*sizeof (int));
Dp[0]=1;
for (int i=1;i<n;i++)
{
int max=1;
for (int j=0;j<i;j++)
{
if (Seq[j]<seq[i])
{
if (max<dp[j]+1)
max=dp[j]+1;
}
}
Dp[i]=max;
}
int ret=dp[n-1];
Delete[] DP;
return ret;
}


////////////////
Template <typename t>
int Lis_greedy (T seq[],int N)
{
int top=0;
int *stack=new Int[n];
memset (stack,0,n*sizeof (int));
STACK[TOP]=SEQ[0];
for (int i=1;i<n;i++)
{
int low=0,high=top;
while (Low<=high)
{
int mid= (Low+high)/2;
if (Stack[mid]<seq[i])
low=mid+1;
Else
High=mid-1;
}
if (low==top+1)
top++;
Stack[low]=seq[i];
}
delete[] Stack;
return top+1;
}


int _tmain (int argc, _tchar* argv[])
{
int testdata[10]={4,6,3,2,5,1,5,7,9,8};
cout<< "LIS by DP:" <<LIS_DP (testdata,10) <<endl;
cout<< "LIS by Greedy:" <<lis_greedy (testdata,10) <<endl;

System ("pause");
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.