杭电 1003题 Max Sum

Max Sum

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

首先分析一下这道题让干什么,从题目中我们应该推出该题是让求一个数组的一个最大子段的和并给出该子段的起始和结束位置。
知道了题目让求什么,接下来就要想办法求出题目要求的值。首先知道数组的值有三种情况,第一全部为正数,第二有正数也有负数,第三全部为负数。

第一种情况比较容易分析,记录数组的起始位置,遍历整个数组,每当sum的值比maxSum值大时,更新maxSum和终止位置,直到数组结束,以下代码便可以解决

    first=1;
    for(int k=0;k<n;k++)
        {
            sum+=a[k];
            if(sum>maxSum)
            {
                maxSum=sum;
                last=k+1;   //k代表下标,最后一个位置为下标+1
            }

        }

第二种情况当数组元素既有正数也有负数时。进一步分情况讨论,有sum>0,sum=0,sum<0三种情况。当sum>=0时,sum可继续向下加,因为sum值遇到正数值会继续增大,那么当sum加到负数变成小于0的数时,此时就应该更新子段的起点,sum归零。因为如果下个数是正数,sum值加上它,值会小于该数。因此可改成如下代码:

for(int k=0;k<n;k++)
        {
            sum+=a[k];
            if(sum>maxSum)
            {
                maxSum=sum;
                first=temp;  //只有当sum值大于maxSum时,临时起点更新为起点
                last=k+1;   //k代表下标,最后一个位置为下标+1
            }
            if(sum<0)
            {
                sum=0;
                temp=k+2;  //更新临时起点
            }
        }

第三种情况,当全部为负数时,同样适用第二种情况的解决方法。

完整代码如下图所示:

#include<iostream>
using namespace std;
int main () {
    int t;  //有t组数据 
    int n;  //每组数据中有n个元素
    int first,last;//代表每组数据最大子段的起始和终止位置
    int sum;  //记录子段的和
    int maxSum;///记录子段的最大值 
    int temp;  //记录可能为最大子元素的起始位置
    int a[100005];

    cin>>t;
    for(int i=0;i<t;i++)
    {
        cin>>n;
        for(int j=0;j<n;j++)
        {
            cin>>a[j];
        }

        //初始化
        first=1;last=1; 
        sum=0;maxSum=-1001;temp=1;

        //查找 
        for(int k=0;k<n;k++)
        {
            sum+=a[k];
            if(sum>maxSum)
            {
                maxSum=sum;
                first=temp;
                last=k+1;   //k代表下标,最后一个位置为下标+1
            }
            if(sum<0)
            {
                sum=0;
                temp=k+2;
            }
        }
        //输出
        cout<<"Case "<<i+1<<":"<<endl;
        cout<<maxSum<<" "<<first<<" "<<last<<endl;
        if(i!=t-1)
        {
            cout<<endl;
        }
    } 
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值