【笨方法学PAT】1007 Maximum Subsequence Sum(25 分)

一、题目

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

二、题目大意

给一个数组,求和最大的连续子序列;如果最大和为0,输出0、第一位和最后一位。

三、考点

最大连续子序列和

四、解题思路

自己的方法比较笨,思路是这样的:

1、直接暴力求解,O(n^3) 肯定会超时,估计这种方案可以一半分数;

2、优化,保存数组的和,之后对和二次遍历,sum[i] - sum[j] > ans_sum,就更新ans_sum、ans_left、ans_right;

五、算法优化

复杂度O(n^3):穷举所有可能的首尾子串组合,用i和j循环确定首尾,k循环累加这之间的数,如果这个组合的累加大于记录过的maxSum则更新。**

复杂度O(n^2):穷举优化版,只用i循环确定子串的首个数,j循环不断延长这个子串,延长的同时累加,如果在累加的某一刻大于了maxSum则更新。

复杂度O(n^2):分治法,将序列分成左右两部分,分别递归求出左,右后,再求跨越中界的最大子序列和。求跨越序列时一直从中间向左/右走到底的同时累加,如果在累加的某一刻大于了maxSum则更新。

复杂度O(n):联机算法,只扫描一遍序列,扫描的同时做累加。判断累加当前元素后的thisSum,如果比记录过的maxSum大则更新,如果比maxSum小且为负,那么丢弃构成thisSum的子串,从下一个元素开始另起一个子串。

大神总结比较经典,包含了核心代码:最大子序列求和问题及联机算法

六、代码

#include<iostream>
#include<vector>
using namespace std;
int main() {
	//读入数据
	int k;
	cin >> k;
	vector<int> vec(k);
	for(int i=0;i<k;++i){
		cin >> vec[i];
	}

	//直接二次遍历,每次都要求和,估计会超时
	vector<int> sum(k+1);
	sum[0] = 0;
	for (int i = 1; i <= k; ++i)
		sum[i] = sum[i - 1] + vec[i-1];

	//求解
	int ans_sum=-9999999, ans_left, ans_right;
	for (int i = 0; i <= k; ++i) {
		for (int j = 0; j < i; ++j) {
			if (sum[i] - sum[j] > ans_sum) {
				ans_sum = sum[i] - sum[j];
				ans_left = j;
				ans_right = i;
			}
		}
	}

	//输出
	if (ans_sum < 0)
		cout << "0 " << vec[0] << " " << vec[k - 1];
	else 
		cout << ans_sum << " " << vec[ans_left] << " " << vec[ans_right-1];
		
	system("pause");
	return 0;
}

七、联机算法

解题思路:在读取数据的过程中,计算sum,如果sum<0,舍弃,重新开始计数;如果sum大,更新输出。

#include<iostream>
#include<vector>
using namespace std;
int main() {
	//读入数据
	int k;
	cin >> k;
	vector<int> vec(k);
	int ans_sum=-1, ans_left=0, ans_right=k-1, tmp_sum=0, tmp_left=0;
	for(int i=0;i<k;++i){
		cin >> vec[i];
		tmp_sum += vec[i];
		//舍弃,重新开始
		if (tmp_sum < 0) {
			tmp_sum = 0;
			tmp_left = i + 1;
		}
		//和增大,更新输出
		else if (tmp_sum > ans_sum) {
			ans_sum = tmp_sum;
			ans_right = i;
			ans_left = tmp_left;
		}
	}

	//输出
	if (ans_sum < 0)
		cout << "0 " << vec[0] << " " << vec[k - 1];
	else 
		cout << ans_sum << " " << vec[ans_left] << " " << vec[ans_right];
		
	system("pause");
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值