杭电ACM1087——Super Jumping! Jumping! Jumping!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087
题目分析:其实就是就最长递增子序列的变种,将输出改成最大的递增子序列的和

#include<stdio.h>
#define M 10010
int a[M];
int dp[M];
int LIS(int nLen)
{
	//a是数据数组
	//dp是递推数组
	//nLen是数组a的长度
	int i,j;
	int nMax = 0;

	for(i = 0; i < nLen; ++i)
	{
		dp[i] = a[i];
		for(j = 0; j < i; ++j)
		{
			if(a[i] > a[j])//如果是严格递增子序列则为>,非严格递增可取>=
			{
				dp[i] = dp[i] > dp[j] + a[i] ? dp[i] : dp[j] + a[i];//所以dp迭代的部分也改成用a[i]的值进行递推
			}
		}
		nMax = nMax > dp[i] ? nMax : dp[i];
	}
	return nMax;
}
void main()
{
	int n;
	int i;
	int nMax;
	while(scanf("%d",&n))
	{
		if(!n)
		{
			break;
		}

		for(i = 0; i < n; ++i)
		{
			scanf("%d",&a[i]);
		}

		nMax = LIS(n);
		printf("%d\n",nMax);
	}
}

你可能感兴趣的:(杭电ACM1087——Super Jumping! Jumping! Jumping!)