Categories
不学无术

1007. Maximum Subsequence Sum (25)

时间限制

400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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

==============================================
这是一个经典问题,可以利用《编程珠玑》第8章<算法设计技术>里面的扫描算法
算法不写了,书上一模一样的原题,伪代码如下:

maxsofar = 0
maxendinghere = 0
for i = [0,n)
    /* invariant: maxendinghere and maxsofar
       are accurate for x[0..i-1] */
    maxendinghere = max(maxendinghere + x[i], 0)
    maxsofar = max(maxendinghere, maxsofar)

只不过这里输出的时候要输出首末位置的数字,所以加了一些变量。
什么时候会改变首末位置呢:
1. maxEndingHere == 0的时候,表示如果后面还会有更好的结果的话,肯定开始的位置是从这个点之后的一个数字开始的。此时要记录可能会产生gap,留给后续处理
2.当到达vStartIndex,别忘了记下<可能的>新的起点
3.当后面那一坨真的比原来的值还大时,如果存在gap,那要用新的起点替换掉旧的,然后吧gap开关关上,一个新的开始
4.当算出来总数maxEndingHere比maxSofar大时,别忘了记下当前位置的数值作为末尾数值
另外要注意,如果全是负值怎么处理,特别注意这样的输入,不能以maxSofar是否>0判断

3
-1 0 -1

代码中startIndex与endIndex是不必要存储的。
=============================================

#include 
#include  //max
using namespace std;
int main()
{
	int N;
	scanf("%d", &N);
	long maxSofar = 0;
	long maxEndingHere = 0;
	int startVal = 0;
	int startIndex = -1;  //
	int vStartIndex = 0;//
	int vStartVal = 0;
	int endIndex = -1;//
	int endVal = 0;
	bool hasGap = true;
	bool allNegative = true;
	//int *savedVals = new int[N];
	int totalStartVal;
	int totalEndVal;
	for(int i=0; i= 0 && allNegative)
			allNegative = false;
		if(i==0)
			totalStartVal = inputVal;
		else if(i == N-1)
			totalEndVal = inputVal;
		//savedVals[i] = inputVal;
		maxEndingHere = max( maxEndingHere + inputVal, (long)0);
		if(maxEndingHere == 0)
		{
			hasGap = true;
			vStartIndex = i+1;
		}
		if(i == vStartIndex)
			vStartVal = inputVal;
		if( maxEndingHere > maxSofar)
		{
			maxSofar = maxEndingHere;
			endIndex = i;
			endVal = inputVal;
			if(hasGap)
			{
				startIndex = vStartIndex;
				startVal = vStartVal;
				hasGap = false;
			}
		}
	}
	if(!allNegative)
		printf("%ld %d %d", maxSofar, startVal, endVal);
	else
		printf("%ld %d %d", maxSofar, totalStartVal, totalEndVal);
	return 0;
}

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 750 2/2
1 答案正确 0 790 1/1
2 答案正确 0 790 3/3
3 答案正确 0 790 4/4
4 答案正确 0 790 4/4
5 答案正确 0 790 3/3
6 答案正确 0 790 3/3
7 答案正确 0 750 5/5

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.