最大连续子段和进阶--(循环数组)

本文探讨了在循环序列中寻找最大连续子段和的问题,提供了两种解题思路:一是通过扩展序列并限制子段长度;二是计算正常最大连续子段和与总和减最小连续子段和的最大值。附带C++实现代码,展示了不同方法的优劣。

交题地址最大循环子段和

题目大意:一段n长度的序列数,围成一圈,问从中取连续的一段子段最大值为多少

解题思路:最大连续子段和,由于是循环的,
思路1:
在n长度的后面在添加一个n-1长度的相同子段,不过这个出来的值有误,比如一般的全正数这个答案不成立,要考虑子段和的长度不能超过n,由于我写的是在线的方法,不好处理,即使是改成移位然后处理的代码显然会TLE(O(n^2))放弃该思路。不过也能过80%数据(O(n))。
思路2:
循环最大连续子段和 = max(正常最大连续子段和,总和-最小连续子段和),再写一个最小连续子段和就AC了


代码如下
思路1(非满分):

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#include <utility>
using namespace std;

typedef long long ll;

const int maxn = (int)2e5+10;
const int inf = 0x3f3f3f3f;

ll table[maxn];

ll solve(ll a[], ll n)
{
	ll i,sum=-inf,Max=-inf;
	ll Mlen = (n+1)/2, Tlen = 0;
	for (i = 0 ; i < n; i++) {
		if(sum >= 0 && Tlen < Mlen) {
			sum += a[i];
			Tlen++;
		}else {
			sum = a[i];
			Tlen = 1;
		}

		if(Max < sum) {
			Max = sum;
		}
	}
	return Max;
}

int main()
{
	ll n,ans;
	scanf("%lld", &n);
	for (ll i = 0; i < n; i++) {
		scanf("%lld", &table[i]);
		table[i+n] = table[i];
	}
	ans = solve(table, 2*n - 1);
	printf("%lld\n", ans);
	return 0;
}

/*
WA:
5
1 2 3 -5 7
*/

思路2:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#include <utility>
using namespace std;

/*
循环最大连续子段和 = max(正常最大连续子段和,总和-最小连续子段和)
*/

typedef long long ll;

const int maxn = (int)1e5+10;
const int inf = 0x3f3f3f3f;

ll table[maxn];

ll solve(ll a[], ll n)
{
	ll i,sum=-inf,Max=-inf;
	for (i = 0 ; i < n; i++) {
		if(sum >= 0) {
			sum += a[i];
		}else {
			sum = a[i];
		}

		if(Max < sum) {
			Max = sum;
		}
	}
	return Max;
}

ll solve2(ll a[], ll n)
{
	ll i,sum=inf,Min=inf;
	for (i = 0 ; i < n; i++) {
		if(sum <= 0) {
			sum += a[i];
		}else {
			sum = a[i];
		}

		if(Min > sum) {
			Min = sum;
		}
	}
	return Min;
}

int main()
{
	ll n,ans,sum = 0;
	scanf("%lld", &n);
	for (ll i = 0; i < n; i++) {
		scanf("%lld", &table[i]);
		sum += table[i];
	}
	ans = max(solve(table, n), sum - solve2(table, n));
	printf("%lld\n", ans);
	return 0;
}
最大连续问题是经典的算法问题之一,目标是从给定的一维数组中找到一个连续数组,使其元素之达到最大值。 ### 解法一:动态规划 (时间复杂度 O(n) ) 我们通过动态规划的思想解决该问题。核心思路是记录当前的最大以及全局最大: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int maxSubArraySum(const vector<int>& nums) { int n = nums.size(); if (n == 0) return 0; // 空数组直接返回 int currentMax = nums[0]; // 当前位置的最大 int globalMax = nums[0]; // 全局最大 for(int i = 1; i < n; ++i){ currentMax = max(nums[i], currentMax + nums[i]); // 如果前面累加的结果小于当前数字本身,则从当前位置重新开始计算 globalMax = max(globalMax, currentMax); // 更新全局最大值 } return globalMax; } int main(){ vector<int> arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; cout << "最大连续: " << maxSubArraySum(arr) << endl; } ``` #### 关键点解释: - **currentMax** 表示以第 `i` 个数结尾的“最大”。如果之前的累积为负数,那么它对后续无贡献,可以选择丢弃。 - **globalMax** 记录了历史过程中遇到过的最大值。 --- ### 解法二:分治法 (Divide and Conquer) 将原数组分为两部分,并递归地求解左右两侧及跨中间的部分最大。 代码较为冗长,在此略去详细实现,但可以作为进一步学习的方向。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胡同的诗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值