(LIS)hdu 1087 Super Jumping! Jumping! Jumping!

题目
hdu1087

题意:
输出数组中的最大递增子序列和(可以不连续)

思路:
LIS的变形,把统计最大递增子序列长度换成统计最大递增子序列和即可(状态dp[ i ] 表示以第 i 个数为结尾的最大递增子序列和)。

代码

#include <bits/stdc++.h>
#define DEBUG freopen("_in.txt", "r", stdin); freopen("_out1.txt", "w", stdout);
using namespace std;
const int MAXN = 1e3 + 10;
int a[MAXN], sum[MAXN];
int main(){
    
    
	int n;
	while (~scanf("%d", &n) && n != 0){
    
    
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]);
		sum[1] = a[1];
		int ans = a[1];
		for (int i = 2; i <= n; i++){
    
    
			sum[i] = 0;
			for (int j = 1; j < i; j++)
				if (a[j] < a[i])
					sum[i] = max(sum[i], sum[j]);
			sum[i] += a[i];
			ans = max(ans, sum[i]);
		}
		printf("%d\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ymxyld/article/details/113754163