1007 Maximum Subsequence Sum [动态规划]

在这里插入图片描述

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
using namespace std;
const int maxn = 10001;
int a[maxn], dp[maxn], s[maxn];
int main()
{
	int n; cin >> n;
	bool flag = false;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		if (a[i] >= 0)
			flag = true;
	}
	if (flag == false)
	{
		cout << "0 " << a[0] <<" "<< a[n - 1]<< endl;
		return 0;
	}
	dp[0] = a[0];
	for (int i = 1; i < n; i++)
	{
		if (dp[i - 1] + a[i] > a[i])
		{
			dp[i] = dp[i - 1] + a[i];
			s[i] = s[i - 1];
		}
		else
		{
			dp[i] = a[i];
			s[i] = i;
		}
	}
	int k = 0;
	for (int i = 1; i < n; i++)
	{
		if (dp[i] > dp[k])
			k = i;
	}
	cout << dp[k] << " " << a[s[k]] << " " << a[k] << endl;
	return 0;
}

原文地址:https://www.cnblogs.com/Hsiung123/p/13811990.html