LIS(n^2) POJ 2533 Longest Ordered Subsequence

题目传送门

题意LIS(Longest Increasing Subsequence)裸题

分析:状态转移方程:dp[i] = max (dp[j]) + 1   (a[j] < a[i],1 <= j < i) 附带有print输出路径函数

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 1e4 + 10;
const int INF = 0x3f3f3f3f;
int a[N], dp[N], fa[N];
int n;

void print(int x)	{
	if (fa[x] != -1)	{
		print (fa[x]);	printf (" %d", a[x]);
	}
	else	printf ("%d", a[x]);
}

void LIS(void)	{
	int ret = 0, last = 0;
	for (int i=1; i<=n; ++i)	{
		dp[i] = 1;	fa[i] = -1;
		for (int j=1; j<i; ++j)	{
			if (a[j] < a[i] && dp[i] < dp[j] + 1)	{
				dp[i] = dp[j] + 1;	fa[i] = j;
			}
		}
		if (ret < dp[i])	{
			ret = dp[i];	last = i;
		}
	}

	printf ("%d
", ret);
	// print (last);	puts ("");
}

int main(void)	{
	while (scanf ("%d", &n) == 1)	{
		for (int i=1; i<=n; ++i)	{
			scanf ("%d", &a[i]);
		}
		LIS ();
	}

	return 0;
}

  

编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4357391.html