hdu - 1087 Super Jumping! Jumping! Jumping!(dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1087

题意:n个数组成的序列中,求最大递增子序列的和。

dp思路:和最长递增子序列类似,只不过dp数组中保存的是序列和而不是序列长度,转移方程为dp=max(dp[i],dp[j]+a[i])。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <set>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 10;
ll dp[maxn];
ll a[maxn];
int main(){
	int n;
	while (cin >> n && n){
		for (int i = 1; i <= n; i++)
			cin >> a[i];
		ll ans = a[1];
		for (int i = 1; i <= n; i++){
			dp[i] = a[i];
			for (int j = 1; j < i; j++){
				if (a[i] > a[j])
					dp[i] = max(dp[i], dp[j] + a[i]);
			}
			ans = max(ans, dp[i]);
		}
		cout << ans << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42792291/article/details/85320376