HDU 1003 Max Sum

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 152176    Accepted Submission(s): 35547


Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
 

Sample Input
  
  
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
 

Sample Output
  
  
Case 1: 14 1 4 Case 2: 7 1 6
 

Author
Ignatius.L

题目意思:给你一个数字序列(有正有负),叫你求其中的一个连续子序列,这个子序列是所有子序列中和最大的。答案输出这个子序列的和,以及这个序列的起始位置。


我在网上搜了一下这道题的题解,感觉他们的解释对于新手来说还是比较难以理解的。所以在这里我仔细分析一下。

设给出的序列是a[1],a[2],,,,a[n]

用b[i]表示以a[i]结尾的连续子序列最大子段和(这个子序列起点不限定,但终点一定为i),那么很显然,最优值一定是max(b[i]),i=1,2,3,,,n。,我们可以用sum来存b[i]的最大值,就是说如果b[i]>sum,那么更新sum,即sum=b[i];显然算法结束时,sum保存的就是最大子段和。

现在关键的问题是求b[i],我们可以这样做

如果b[i]>=0,那么就让b[i]=b[i-1]+a[i],否则b[i]=a[i],并更新起点;

为什么这样做呢,因为如果你的b[i]小于0了,你再加上一个数,那么最后它们的和肯定小于这个数,我还不如直接从这个数开始呢,是不是这个道理?

此时如果有b[i]>sum,那么更新sum,同时也要更新起始位置

真正实现的时候,由于a和b可以重复利用,我们没必要开数组。

以求0 6 -1 1 -6 7 -5的最大子段和为例

假设当前子段和为b,最优子段和为sum;并将它们初始化为负无穷,但是计算机不能表示负无穷,我们只能以一个比较小的数代替,这里将他们都初始化为b=-999999,sum=-999999

更新过程如下

a06-11-67-5
b0656072
sum0666677
start1111111
end1234567


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<queue>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
using namespace std;
const int maxn=1005,maxe=100005,inf=1<<29;//头文件和这个可以不用写的,但是我比较懒,直接套模板
int n,m;
int main()
{
    int t,total;
    scanf("%d",&t);
    total=t;
    while(t--)
    {
        scanf("%d",&n);
        int start=1,a,b=-999999,sum=-999999,besti,bestj;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            if(b>=0) b+=a;
            else b=a,start=i;
            if(sum<b) sum=b,besti=start,bestj=i;//逗号乃缩减代码神器
        }
        printf("Case %d:\n",total-t);
        printf("%d %d %d\n",sum,besti,bestj);
        if(t) puts("");//最后一行不用输出空行喽
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值