最大连续子序列的和


暴力枚举法:O(n^3) 级复杂度, 可以求出最大连续子序列的范围;

直接两个for循环枚举子序列的首尾,然后再来个循环计算序列的和,每次更新和的最大值。

#include 
using namespace std;
#define N 100005
#define INF 0x3f3f3f3f

int num[N], left, right;

int Sum (int a[], int st, int ed)   //求连续区间 [st, ed] 之间的和
{
	int sum=0;
	for (int i=st; i<=ed; i++)
		sum += a[i];
	return sum;
}

// 枚举所有可能的区间,找到其中有最大值的区间即可
int MaxSubSum (int a[],  int n)
{
	int Max = -INF;
	for (int i=0; i Max)
			{
				Max = cursum;
				left = i; right = j;
			}
		}
	}
	return Max;
}

int main ()
{
	int t, n;
	freopen ("test.txt","r",stdin);
	scanf ("%d",&t);
	while (t--)
	{
		scanf ("%d",&n);
		for (int i=0; i


优化后的穷举算法:O(n^2) 级复杂度, 可以求出最大连续子序列的范围;

算法和上一个什么改变,就是把求和的循环预处理了一下。从而可以在每次枚举区间的时候直接算出,而不用每次都循环得出区间的和。

#include 
#include 
using namespace std;
#define N 100005
#define INF 0x3f3f3f3f

int sum[N], num[N];
int left, right;

int MaxSubSum (int n)
{
	int maxsum = -INF;
	for (int i=0; i maxsum)
			{
				maxsum = cursum;
				left = i, right = j;
			}
		}
	}
	return maxsum;
}

int main ()
{
	int t, n;
	freopen ("test.txt","r",stdin);
	scanf ("%d",&t);
	while (t--)
	{
		scanf ("%d",&n);
		memset (sum, 0, sizeof sum);
		int tmp = 0;
		for (int i=0; i


动态规划法:O(n) 级复杂度, 可以求出最大连续子序列的范围;

#include 
#include 
using namespace std;
#define N 100005
#define INF 0x3f3f3f3f

int num[N];
int left, right;

int MaxSubSum (int a[], int n)
{
	int maxsum = -INF;
	int st=0, cursum=0;
	for (int i=0; i maxsum)
		{
			maxsum = cursum;
			left = st;
			right = i;
		}
	}
	return maxsum;
}

int main ()
{
	int t, n;
	freopen ("test.txt","r",stdin);
	scanf ("%d",&t);
	while (t--)
	{
		scanf ("%d",&n);
		for (int i=0; i



你可能感兴趣的:(递归,动态规划,优化)