求解最长上升子序列的三种方法

动态规划O(N^2)

这种算法主要是依靠前面已经得出的最长递增子序列,来推出当前的最长递增子序列。

例:当前遍历到m时,值为n

(1)dp[ m ]=1,n为当前最大值

(2)dp[ m ]=max(dp[ m ],dp[ 1~ m]+1),n要大于当前的数才能进行比较,+1表示把m点也算入序列中

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1010;
int t[N];
int dp[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&t[i]);
    int ant=1;
    for(int i=1;i<=n;i++)
    {
        dp[i]=1;
        for(int j=0;j<i;j++)
            if(t[j]<t[i])
            {
                dp[i]=max(dp[i],dp[j]+1);
                ant=max(ant,dp[i]);
            }
                
    }
    printf("%d\n",ant);
    return 0;
}

输出序列:可以设置一个数组来记录第 i 个数的递增子序列由哪个数得到,类似于并查集 

DP+二分NlogN

DP+二分 分为两种情况,当遍历到第i个数m时,m比维护的递增序列数组最后一个数还要大时,将m增加到数组末尾中,数组长度+1。否则,在数组中二分查找刚好大于m的数,并替换。

例:假设存在一个序列d[1..9] ={ 2,1 ,5 ,3 ,6,4, 8 ,9, 7},可以看出来它的LIS长度为5。
下面一步一步试着找出它。
我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列。
此外,我们用一个变量Len来记录现在最长算到多少了

首先,把d[1]有序地放到B里,令B[1] = 2,就是说当只有1一个数字2的时候,长度为1的LIS的最小末尾是2。这时Len=1

然后,把d[2]有序地放到B里,令B[1] = 1,就是说长度为1的LIS的最小末尾是1,d[1]=2已经没用了,很容易理解吧。这时Len=1

接着,d[3] = 5,d[3]>B[1],所以令B[1+1]=B[2]=d[3]=5,就是说长度为2的LIS的最小末尾是5,很容易理解吧。这时候B[1..2] = 1, 5,Len=2

再来,d[4] = 3,它正好加在1,5之间,放在1的位置显然不合适,因为1小于3,长度为1的LIS最小末尾应该是1,这样很容易推知,长度为2的LIS最小末尾是3,于是可以把5淘汰掉,这时候B[1..2] = 1, 3,Len = 2

继续,d[5] = 6,它在3后面,因为B[2] = 3, 而6在3后面,于是很容易可以推知B[3] = 6, 这时B[1..3] = 1, 3, 6,还是很容易理解吧? Len = 3 了噢。

第6个, d[6] = 4,你看它在3和6之间,于是我们就可以把6替换掉,得到B[3] = 4。B[1..3] = 1, 3, 4, Len继续等于3

第7个, d[7] = 8,它很大,比4大,嗯。于是B[4] = 8。Len变成4了

第8个, d[8] = 9,得到B[5] = 9,嗯。Len继续增大,到5了。

最后一个, d[9] = 7,它在B[3] = 4和B[4] = 8之间,所以我们知道,最新的B[4] =7,B[1..5] = 1, 3, 4, 7, 9,Len = 5。

于是我们知道了LIS的长度为5。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
const int inf=0X3f3f3f3f;
int tmp[N];
int n;
int er_left(int r,int x)
{
	int l=1,mid;
	
	while(l<r)
	{
	    mid=l+r>>1;
		if(tmp[mid]>=x)
			r=mid;
		else
			l=mid+1;
	}
	return r;
}
int main()
{
	scanf("%d",&n);
	int ans=0;
	int c;
	tmp[0]=-inf;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&c);
		if(c>tmp[ans])
			tmp[++ans]=c;
		else
			tmp[er_left(ans,c)]=c;
	}
	printf("%d\n",ans);
	return 0;
}

输出序列:可以设置两个数组,pos[ ],pre[ ]。pos用来记录tmp位置上是第几个数,pre用来记录每个数的前一个数 

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
const int inf=0X3f3f3f3f;
int a[N];
int tmp[N];
int pos[N];
int pre[N]; 
int n;
int er_left(int r,int x)
{
	int l=1,mid;
	
	while(l<r)
	{
	    mid=l+r>>1;
		if(tmp[mid]>=x)
			r=mid;
		else
			l=mid+1;
	}
	return r;
}
int main()
{
	scanf("%d",&n);
	int ans=0;
	int c;
	tmp[0]=-inf;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&c);
		a[i]=c;
		if(c>tmp[ans])
		{
			tmp[++ans]=c;
			pos[ans]=i;
			pre[i]=pos[ans-1];
		}
		else
		{
			int t=er_left(ans,c);
			tmp[t]=c;
			pos[t]=i;
			pre[i]=pos[t-1];
		}		

	}
	printf("%d\n",ans);
	while(pos[ans]>=1)
	{
		printf("%d ",a[pos[ans]]);
		pos[ans]=pre[pos[ans]];
	}
	return 0;
}

手动模拟一下,比较容易理解 

LCS:最后一种方法是用LCS来求,先将序列排序,求序列与排序后序列的最长公共子序列即可。

LCS模板

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chp的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值