Maximum sum (最大子段和问题)

Given a set of n integers: A={a1, a2,…, an}, we define a function d(A) as below:
在这里插入图片描述
Your task is to calculate d(A).

Input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, …, an. (|ai| <= 10000).There is an empty line after each case.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input
1
10
1 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.
Huge input,scanf is recommended.

题目描述:给出一段序列,包含n个数。在这段序列中找出两端子序列,使得这两段子序列的和最大。

题目分析:
这是一道最大子段和的延伸问题。
我们可以分别求出左往右和从右往左的最大子段和,然后再遍历一遍寻找最大值即可。
下面以从左往右为例讲一下怎么求最大子段和。

  1. 状态表示:f[i] //表示1-i这一段序列中包含的最大子段和是多少
  2. 状态计算:可以用sum来表示当前的子段和为多少。让sum与f[i-1]进行比较,保留最大值即可。
    状态转移方程为:f[i]=max(f[i-1],sum+a[i])

代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map> 
#include <queue>
#include <algorithm>
#include <iomanip>
#define LL long long
const int N=5e4+5;
using namespace std;
int a[N],b[N],c[N];     //用b[]来储存从左往右的最大子段和
int main()              //用c[]来储存从右往左的最大子段和
{
	int t;
	scanf("%d",&t);     //输入数据较大,推荐使用scanf
	while(t--)
	{
		memset(a,0,sizeof a);   //先将三个数组清空
		memset(b,0,sizeof b);
		memset(c,0,sizeof c);
	    int n,ans=-1e9;         //答案可能为负数,因此ans要为-1e9
    	scanf("%d",&n);
    	
    	for(int i=1;i<=n;i++)
	    scanf("%d",&a[i]);
	    
	    b[0]=-1e9;     //预处理,保证b[0]不会被取到
	    int sum=0;     //当前子段和
    	for(int i=1;i<=n;i++)
	    {
	    	b[i]=max(b[i-1],sum+a[i]);  //状态转移方程
	    	sum+=a[i];
	    	if(sum<0) sum=0;   //如果sum<0,为了保证sum子段和最大
    	}                      //要重新开始计算(即sum=0)
    	//同理,求从右往左的最大子段和
    	c[n+1]=-1e9;  
    	sum=0;
	    for(int i=n;i>0;i--)
    	{
    		c[i]=max(c[i+1],sum+a[i]);
    		sum+=a[i];
    		if(sum<0) sum=0;
    		ans=max(ans,b[i]+c[i+1]);   //遍历求最大值
	    }
	    printf("%d\n",ans);
	}
	return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Maximum Sum Increasing Subsequence (最大上升子序列和)问题可以使用动态规划来解决,以下是使用C语言实现的代码: ```c #include <stdio.h> int max(int a, int b) { return a > b ? a : b; } int maxSumIncreasingSubsequence(int arr[], int n) { int i, j, maxSum = 0; int dp[n]; // 初始化dp数组为每个元素自身 for(i = 0; i < n; i++) { dp[i] = arr[i]; } // 动态规划计算最大上升子序列和 for(i = 1; i < n; i++) { for(j = 0; j < i; j++) { if(arr[j] < arr[i]) { dp[i] = max(dp[i], dp[j] + arr[i]); } } } // 找到dp数组中的最大值 for(i = 0; i < n; i++) { if(maxSum < dp[i]) { maxSum = dp[i]; } } return maxSum; } int main() { int arr[] = {1, 101, 2, 3, 100, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); printf("最大上升子序列和为 %d", maxSumIncreasingSubsequence(arr, n)); return 0; } ``` 输出结果为: ``` 最大上升子序列和为 106 ``` 上述代码中,首先定义了一个 `max` 函数,用于比较两个数中的较大值。然后定义了一个 `maxSumIncreasingSubsequence` 函数,用于计算最大上升子序列和。该函数使用一个一维数组 `dp` 来记录从第一个元素到当前元素的最大上升子序列和。在初始化 `dp` 数组时,将每个元素的值初始化为自身;然后使用两层循环遍历数组,如果当前元素大于前面的元素,则将 `dp[i]` 更新为 `dp[j] + arr[i]` 的较大值;最后在 `dp` 数组中找到最大值即为最大上升子序列和。最后在 `main` 函数中定义一个整型数组 `arr`,并计算其最大上升子序列和并输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lwz_159

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

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

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

打赏作者

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

抵扣说明:

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

余额充值