PAT-ADVANCED1007/Data Structures and Algorithms7-1——Maximum Subsequence Sum

我的PAT-ADVANCED代码仓

https://github.com/617076674/PAT-ADVANCED

我的Data Structures and Algorithms代码仓

https://github.com/617076674/Data-Structures-and-Algorithms

原题链接

PAT-ADVANCED1007:https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168

Data Structures and Algorithms7-1:https://pintia.cn/problem-sets/16/problems/663

题目描述

PAT-ADVANCED1007、Data Structures and Algorithms7-1:

题目翻译

1007 最大连续子序列和

给定一个由K个整数{N1, N2, ..., Nk}组成的序列。一个连续的子序列被定义为{Ni, Ni + 1, ..., Nj},其中1 <= i <= j <= K。最大子序列是一个连续的子序列,且其和最大。举个例子,给出序列{-2, 11, -4, 13, -5, -2},其最大子序列是{11, -4, 13},其最大和是20。

现在你需要找到这个最大和,以及该最大子序列的第一个元素和最后一个元素。

输入格式:

每个输入文件包含一个测试用例。每个测试用例占两行。第一行给出一个正整数K(<= 10000)。第二行包含K个数字,每个数字由一个空格分隔。

输出格式:

对每个测试用例,在一行中输出最大总和,以及最大子序列的第一个和最后一个数字。数字必须用一个空格分隔,但在一行的末尾不能有多余的空格。在最大子序列不唯一的情况下,输出具有最小索引i和j的那个(如示例情况所示)。 如果所有K数都是负数,则其最大总和定义为0,并且你应该输出整个序列的第一个和最后一个数字。

输入样例:

10
-10 1 2 3 4 -5 -23 3 7 -21

输出样例:

10 1 4

知识点

动态规划

思路一

记录前序和

首先用sum数组记录序列的前序和,sum[i] = nums[0] + ... + nums[i],为避免越界,所有数据用long long型存储。

对每个索引位置,寻求其子序列和最大的右端索引。

时间复杂度是O(K ^ 2)。空间复杂度是O(K)。

C++代码

#include<iostream>

int main() {
	int K;
	scanf("%d", &K);
	long long nums[K];
	for(int i = 0; i < K; i++) {
		scanf("%lld", &nums[i]);
	}
	long long sum[K];	//sum[i] = nums[0] + ... + nums[i]
	sum[0] = nums[0];
	for(int i = 1; i < K; i++) {
		sum[i] = sum[i - 1] + nums[i];
	}
	long long max = -1;
	int left = -1, right = -1;
	for(int i = 0; i < K; i++) {
		if(i == 0) {
			for(int j = i; j < K; j++) {
				if(sum[j] > max) {
					max = sum[j];
					left = 0;
					right = j;
				}
			}
		} else {
			for(int j = i; j < K; j++) {
				if(sum[j] - sum[i - 1] > max) {
					max = sum[j] - sum[i - 1];
					left = i;
					right = j;
				}
			}
		}
	}
	if(max < 0) {
		printf("0 %lld %lld\n", nums[0], nums[K - 1]);
	} else {
		printf("%lld %lld %lld\n", max, nums[left], nums[right]);
	}
	return 0;
}

C++解题报告

思路二

在线处理

根据题目的最后一句话,如果所有的数字都是负数,则最大和定义为0。我们可以把题意理解为求子序列和与0的较大值。

因此我们只遍历一遍数组,如果其和sum大于了最大值max,我们更新max的值,令left索引等于tempLeft(之前记录的左端索引),令right索引等于i(当前索引值)。如果其和sum小于0,我们更新tempLeft值为i + 1,即以下一个位置作为子序列的起点,同时更新sum值为0。因为对于sum为负数的部分,我们是要全部舍去的,舍去这部分,从下一个索引开始计算能得到更大的子序列和。

时间复杂度和空间复杂度均是O(K)。

C++代码

#include<iostream>

int main() {
	int K;
	scanf("%d", &K);
	long long nums[K];
	for(int i = 0; i < K; i++) {
		scanf("%lld", &nums[i]);
	}
	long long sum = 0, max = -1;
	int left, right, tempLeft = 0;
	for(int i = 0; i < K; i++) {
		sum += nums[i];
		if(sum > max) {
			max = sum;
			left = tempLeft;
			right = i;
		}
		if(sum < 0) {
			tempLeft = i + 1;
			sum = 0;
		}
	}
	if(max < 0) {
		printf("0 %lld %lld\n", nums[0], nums[K - 1]);
	} else {
		printf("%lld %lld %lld\n", max, nums[left], nums[right]);
	}
	return 0;
}

C++解题报告

思路三

动态规划

状态定义

f(x) -------- 以索引x结尾的连续子序列的最大和;g(x) -------- 以索引x结尾的拥有最大和的连续子序列的左端索引。

状态转移

当x == 0时,f(x) = nums[0],g(x) = 0。

当x > 0时,如果nums[x] > nums[x] + f(x - 1),f(x) = nums[x],g(x) = x。否则,f(x) = nums[x] + f(x - 1),g(x) = g(x - 1)。

再输出f(x)中的最大值即为最大和,再根据这个最大和对应的索引来寻找其左端索引,而其右端索引就是该最大和对应的索引自己。

时间复杂度和空间复杂度均是O(K)。

C++代码

#include<iostream>

int main() {
	int K;
	scanf("%d", &K);
	long long nums[K];
	for(int i = 0; i < K; i++) {
		scanf("%lld", &nums[i]);
	}
	long long dp[K];	//dp[i]表示以nums[i]结尾的最大序列和
	int left[K];	//left[i]表示以nums[i]结尾的最大序列的左端索引 
	dp[0] = nums[0];
	left[0] = 0; 
	for(int i = 1; i < K; i++){
		if(nums[i] > nums[i] + dp[i - 1]){
			dp[i] = nums[i];
			left[i] = i;
		}else{
			dp[i] = nums[i] + dp[i - 1];
			left[i] = left[i - 1];
		}
	}
	int max = 0;
	for(int i = 1; i < K; i++){
		if(dp[i] > dp[max]){
			max = i;
		}
	}
	if(dp[max] < 0){
		printf("0 %lld %lld\n", nums[0], nums[K - 1]);
	}else{
		printf("%lld %lld %lld\n", dp[max], nums[left[max]], nums[max]);
	} 
	return 0;
}

C++解题报告

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值