【PAT】1007 Maximum Subsequence Sum (25) 最大连续子序列和 +记录位置

题目

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
题意:求最大连续子序列和并记录该序列的头尾元素



方法一:

 复杂度:O(n^2),这题数据较小可过
 思路: sum(i,j+1)=sum(i,j)+a[j+1],节省了一些重复运算

#include<iostream>
#include<cstdio>
using namespace std;
#define maxN 10001
#define Inf 0x3f3f3f

int main()
{	
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int a[maxN];
	int k,sum,max=-Inf;
	int L, R;
	cin >> k;
	for (int i = 0; i < k; i++)
		cin >> a[i];

	
	for (int i = 0; i < k; i++)
	{
		sum = 0;
		for (int j = i; j < k; j++)
		{
			sum += a[j];
				if (sum>max)
				{
					max = sum;
					L = i;
					R = j;
				}
		}
	}
	if (max<0)	//全为负数时,按=0处理,输出头尾
		cout << "0" << " " << a[0] << " " << a[k-1] << endl;
	else
	cout << max << " " <<a[L] << " " << a[R] << endl;
	return 0;
}
/*
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4

*/


方法二:

复杂度:O(n)
思路::最大连续子序列和问题,动态规划法(参考http://www.tuicool.com/articles/NfmyIf)
注意点:

       1、开始位置和结束位置的记录 

       2、全为负数时,按=0处理,输出头尾

       3、对于-1 0 0 -1   应输出0 0 0 

#include<iostream>
#include<cstdio>
using namespace std;
#define maxN 10001
#define Inf 0x3f3f3f

int main()
{	
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int a[maxN];
	bool flag = false; //标记maxSum是否一直未增加
	bool flag_ = false;//标记是否出现-1 0 0 -1的情况
	int k, sum, maxSum;//maxSum最大子序列
	sum = maxSum = 0;
	int L, R,t=0;
	cin >> k;
	for (int i = 0; i < k; i++)
		cin >> a[i];
	for (int i = 0; i < k; i++)
	{		
			sum += a[i];
			if (sum>=maxSum)//若sum不断增加,则maxSum不断更新,若遇到负数,sum会较小(小于maxSum),则不更新maxSum
			{		
				if (sum>maxSum)
				{				
				maxSum = sum;
				L = t;//sum降到<0后重新开始且sum是在增加的位置
				R = i;//最大子序列结尾	
				flag = true;
				}
				else
				{   //当为-1 0 0 -1的情况,记录0开始的位置和结束位置
					if (sum == 0 && maxSum == 0)
					{
						L = t;
						R = i;
						flag_ = true;
					}
				}
			}
			else if (sum < 0)//若sum降到0了,则抛弃前面已经扫描过的那一段,sum置为0
			{								
				t = i + 1;//sum降到<0后重新开始的位置				
				sum = 0;				
			}
		
	}
	if (!flag&&!flag_)//若全为负数
		cout << "0" << " " << a[0] << " " << a[k - 1] << endl;
	else
	cout << maxSum << " " << a[L] << " " << a[R] << endl;

	cin >> sum;

	return 0;
}
/*
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4

Sample Input:
4
-1 0 0 -1
Sample Output:
0 0 0

*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值