动态规划01-复杂度2 Maximum Subsequence Sum (25 分)

01-复杂度2 Maximum Subsequence Sum (25 分)

题目来自PTA

英文题目:

Given a sequence of K integers { N​1, N​2, …, N​K}. A continuous subsequence is defined to be { N​i,N​i+1,…, N​j} 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

题目目标->求最大子序列

注意事项:
1、 在最大子序列不唯一的情况下,输出具有最小索引i和j的那个(输出排在前面的序列)。
2、如果所有K个数都是负数,那么它的最大和被定义为0,你应该输出整个序列的第一个和最后一个数。

题解:

除了要输出最大子序列开始和末尾的数字,这道就是一道简单的动态规划

1、定义两个整型变量max和now。分别代表目前的子序列和和到现在为止的最大和,初始值都为0
(正常来说初始值要往小里设,避免最大值为负数的情况,但这道题全为负数时,要按规定输出0,不会出现最大和为负数的情况)。
2、从头到尾遍历数组定义最大子序列,动态更新now和max。
每遍历一个数后,得到当前子序列和,当now小于0,必然无法对max做出贡献,置为0,当now为正数时与max比较大小,更新最大值。

除了求出最大子序列和,还要标记最大子序列的开始和结束的位置

1、结束的位置很好找,每当now大于max,更新最大值时,当前遍历的位置必然是构成当前max子序列的结尾。
2、对于开始的位置,我的方法是找出结束位置和开始位置之间的差值
我设置了两个int型的变量s和sx。
前者是当前的子序列的末尾和开头索引间的差值,后者是当前最大子序列的开头结尾索引间的差值。
s随着数组的遍历增加一,当now小于0时,将s置为一,因为now小于0必然无法对max做出贡献,当now大于max时,再将s的值赋给sx,得到差值。

除此之外,还有特殊情况(总共情况有三):

1、全为负数,输出0 第一个和最后一个数字
2、只有负数和零,输出"0 0 0"
3、普通情况,输出最大和 以及起始和结束元素值

// C++代码
//题目中有K的最大值,可以将动态数组变成int nums[10005]
#include <vector>
#include <iostream>
#iCnclude <cstdio>
using namespace std;

int main() {

	//因为题目有给出数字最大数目,这里也可以直接建一个int类型的数组,
	vector<int> nums;//int nums[10005];
	int K;
	cin >> K;//读取输入数字个数

	bool hfz = true;//hfz为true代表输入中都是负数或0
	bool hz = false;//hz为false代表输入中不存在0
	for (int i = 0; i < K; i++)
	{
		int tmp;
		cin >> tmp;
		nums.push_back(tmp);
		if (nums[i] > 0) hfz = false;//判断读入数字成分:0、正数、负数
		if (nums[i] == 0) hz = true;
	}
	int now = 0, m = 0, s=0, e=0,sx = 0;

	for (int i = 0; i < K; i++)
	{
		now += nums[i];
		s++;
		if (now < 0) {
			now = 0;
			s = 0;
		}
	
		if (now > m) {
			e = i;m = now;sx = s - 1;
		}
		
	}
	if (hfz&&!hz)cout << 0 << " " << nums[0] << " " << nums[K-1] << endl;
	else if (hfz&&hz) cout << 0 << " " << 0 << " " << 0 << endl;
	else printf("%d %d %d\n", m, nums[e-sx], nums[e]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/awsl_6699/article/details/105983128