LIS路径记录(UVA481)

6 篇文章 0 订阅

出自一次很失败的开学测试

LIS自然会做

可以参见:https://blog.csdn.net/Radium_1209/article/details/79704234

由于对于LIS的nlogn算法不熟悉,导致错误理解,记录的路径出现了问题,其中还用了n^2的算法记录路径(好理解),但是不出所料T了。

正确的方法应该是:由于nlogn算法中,dp[i]存的是长度为i的最后一位数字,并不是路径!!!我们可以用一个path数组来标记每一个数字位于的位置,然后通过dfs倒着来找最后结果长度的最后一个,或者不用dfs直接找也行。

 

例题:(UVA481)

Write a program that will select the longest strictly increasing subsequence from a sequence of integers.

Input

The input file will contain a sequence of integers (positive, negative, and/or zero). Each line of the input file will contain one integer.

Output

The output for this program will be a line indicating the length of the longest subsequence, a newline, a dash character ('-'), a newline, and then the subsequence itself printed with one integer per line. If the input contains more than one longest subsequence, the output file should print the one that occurs last in the input file.

Notice that the second 8 was not included -- the subsequence must be strictly increasing.

Sample Input

-7
10
9
2
3
8
8
1

Sample Output

4
-
-7
2
3
8

题意:求LIS并输出路径(最后一个)

dfs版:

#include <cstdio>
#include <iostream>
using namespace std;

int n=1,a[1000005];
int dp[1000005];
int path[1000005];

void dfs(int i,int x)
{
	if (i<1 || x<=0)
		return;
	while(path[i]!=x)
		i--;
	dfs(i,x-1);
	printf("%d\n",a[i]);
}

int main()
{
	while(~scanf("%d",&a[n]))
	{
		n++;
	}
	n--;
	int len=1;
	dp[1]=a[1]; path[1]=1;
	for (int i=2;i<=n;i++)
	{
		if (a[i]>dp[len])
		{
			dp[++len]=a[i];
			path[i]=len;
		}
		else
		{
			int pos=lower_bound(dp+1,dp+len+1,a[i])-dp;
			dp[pos]=a[i];
			path[i]=pos;
		}
	}
	printf("%d\n-\n",len);
	dfs(n,len);
	return 0;
}

 

非dfs版:

#include <cstdio>
#include <iostream>
using namespace std;

int n=1,a[1000005];
int dp[1000005];
int path[1000005];
int ans[1000005];

int main()
{
	while(~scanf("%d",&a[n]))
	{
		n++;
	}
	n--;
	int len=1;
	dp[1]=a[1]; path[1]=1;
	for (int i=2;i<=n;i++)
	{
		if (a[i]>dp[len])
		{
			dp[++len]=a[i];
			path[i]=len;
		}
		else
		{
			int pos=lower_bound(dp+1,dp+len+1,a[i])-dp;
			dp[pos]=a[i];
			path[i]=pos;
		}
	}
	printf("%d\n-\n",len);
	int temp=n;
	int l=len;
	while(l>=1)
	{
		int i;
		for(i=temp;i>=1;i--)
		{
			if (path[i]==l)
			{
				ans[l]=a[i];
				l--; temp=i;
				break;
			}
		}
		if (i==0)
			break;
	}
	for (int i=1;i<=len;i++)
		printf("%d\n",ans[i]);
	return 0;
}

 

n^2版本(T了,正确性未知)

#include <cstdio>
#include <iostream>
using namespace std;

int n=1,a[1000005];
int dp[1000005];
int path[1000005];
int ans[1000005];

int main()
{
	while(~scanf("%d",&a[n]))
	{
		n++;
	}
	n--;
	int len=0;
	for (int i=1;i<=n;i++)
	{
		dp[i]=1;
		for (int j=1;j<i;j++)
			if (a[j]<a[i])
			{
				if (dp[i]<dp[j]+1)
				{
					dp[i]=dp[j]+1;
					path[dp[i]]=j;
				}
			}
		if (dp[i]>=len)
		{
			len=dp[i];
			ans[len]=a[i];
			for (int i=len;i>=2;i--)
				ans[i-1]=a[path[i]];
		}
	}
	printf("%d\n-\n",len);
	for (int i=1;i<=len;i++)
		printf("%d\n",ans[i]);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值