PTA甲级1007

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

结尾无空行

思路:

1、最后要输出i到j的sum和最大,定义一个sum[K],其第i项sum[i]表示前i项的sum和,那么题目中所求转化成了sum[j]-sum[i]最大,对应两种办法

(1)暴力,直接两层循环嵌套,找出最大的res=sum[j]-sum[i],并记录下此时的start位置和end位置,时间复杂度为O(n^2)

for (int i = 0; i < K; i++) {
		for (int j = i + 1; j <= K; j++) {
			if (sum[j] - sum[i] > res) {
				res = sum[j] - sum[i];
				start = num[i + 1];
				end = num[j];
			}
		}
	}

(2)只用一层循环,定义一个low和high变量,low赋初值为0,high作为循环遍历的变量,从0到K,当sum[high]-sum[low]的值比res大时,res=sum[high]-sum[low],并且记录此时的start和end位置。若sum[low]>sum[high],则表明此时的第high项很小,low=high,然后继续遍历操作寻找正确的res值。

for (int high = 1; high <= k; high++) {
		if (sum[high] - sum[low] > res) {
			res = sum[high] - sum[low];
			start = num[low + 1];
			end = num[high];
		}
		if (sum[low] > sum[high]) {
			low = high;
			//high++;
		}
	}

Tips:无论是暴力还是优化,都要从数组下标0开始,虽然数组内容的存放是从1到K,但是可以发现start记录的位置是i+1或者low+1,如果第一项是很大的数,那么从下标1开始,start最多只能记录到num[2]的位置,也是因为这个原因之前有几个测试点过不去。

完整代码如下:

#include <iostream>
using namespace std;

int num[10002], sum[10002];
int main() {
	int K;
	cin >> K;
	sum[0] = 0;
	for (int i = 1; i <= K; i++) {
		cin >> num[i];
	}
	for (int i = 1; i <= K; i++) {
		sum[i] = sum[i - 1] + num[i];
	}
	int res = -1;
	int start, end;
	//int low = 0;
	//for (int high = 1; high <= k; high++) {
	//	if (sum[high] - sum[low] > res) {
	//		res = sum[high] - sum[low];
	//		start = num[low + 1];
	//		end = num[high];
	//	}
	//	if (sum[low] > sum[high]) {
	//		low = high;
	//		//high++;
	//	}
	//}
    
	for (int i = 0; i < K; i++) {
		for (int j = i + 1; j <= K; j++) {
			if (sum[j] - sum[i] > res) {
				res = sum[j] - sum[i];
				start = num[i + 1];
				end = num[j];
			}
		}
	}
    
	if (res < 0) {
		cout << "0 " << num[1] << " " << num[K];
	}
	else cout << res << " " << start << " " << end;
}

注释部分是方法2,下面的循环嵌套是方法1,两个都能25分AC。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值