1007. Maximum Subsequence Sum (25)

1.题目要求最大的连续自序列和,如果全为负数,则输出0,首位,末位;如果不为负数,则输出总和,子序列的第一位,子序列的最后一位。

2.注意判断全为负数的情况,第一个数是否为负数,不要漏判断。(刚开始漏判断了)

3.实际上采用动态规划,dp[i]=max{dp[i-1]+num[i],num[i]},dp[i]一定会包含当前的数字num[i],但是可以简化为判断dp[i-1]是否为负数。

Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
using namespace std;
int main(void) {

	int n;
	cin >> n;
	if (n == 0)
	{
		cout << "0 0 0" << endl;
		return 0;
	}
	long long *num = new long long[n];
	for (int i = 0; i < n; i++)
	{
		cin >> num[i];
	}
	//使用pair,first代表从那个i开始,second表示从i加到当前数字的综合
	vector<pair<int, long long>> dp(n, { 0, 0 });
	dp[0].first = 0;
	dp[0].second = num[0];
	bool negative = true;
	if (num[0] >= 0) negative = false;//注意第一个数也要判断正负
	for (int i = 1; i < n; i++)
	{
		if (negative&&num[i] >= 0) negative = false;//判断是否全负数
		if (dp[i - 1].second <= 0)
		{//如果dp[i-1]小于0,那么num[i]加上它肯定小于num[i],所以不加
			dp[i].first = i;
			dp[i].second = num[i];
		}
		else
		{//如果dp[i-1]大于等于0,那么num[i]加上它肯定大于等于num[i],所以需要加上
			dp[i].first = dp[i - 1].first;
			dp[i].second = dp[i - 1].second + num[i];
		}

	}

	if (negative)
	{//如果全为负数,则输出首尾两个数
		cout << 0 << " " << num[0] << " " << num[n - 1] << endl;
	}
	else
	{
		long long maxAns = -1; int index = -1;
		for (int i = 0; i < n; i++)
		{//遍历一遍,找出second最大的dp
			if (maxAns < dp[i].second)
			{
				maxAns = dp[i].second;
				index = i;
			}
		}
		//根据题目要求输出最大的和,和相当一序列起始值和结束值
		cout << dp[index].second << " " << num[dp[index].first] << " " << num[index] << endl;
	}

	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注