HDU 1003 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
题目大意讲的是给你一长串的数据,你取出其中的一段有序子列,使其和最大。数据范围在1~100000之间,一开始的想法是深搜,一想绝对超时。
思路大意是这样的:
1.首先我们先不去想多个数据,化简题目,假如给你一个数字,让你判断是否放到子列中去呢,那问题就很简单了。当然是,如果是负数我就不加,是正数我就加进去呗,因为要使和最大嘛,我们首先得到个结论,要想让子列和最大,其中一定不能有负数。
2.那么接着在想,我们能否把一段数字看成一个数字呢,例如 6,2,-11这三个数之和其实也可以看成-3,那我们算到这里就没有必要把这三个数和接下来的数字加在一起了,因为-3 加上任何数只能使其更小
3.那总的思路就是这样,我从第一个开始,一个一个往后面加,实时更新最大和max,如果和为非负,就继续加下去,如果出现和为负数的情况,那之前的子列就没有必要再往后加了,那就重新开始新的子列。g
4.举个例子:
有9个数 7,-3,9,-17,11,-2,-4,17,-6
第一遍 得到7 ,sum=0+7=7, 7>-9999, max=7,head_max=0;tail_max=0(这里从零开始计数)7>0 继续加,
第二遍 得到-3, sum=7+(-3)=4,4<7,则max不更新,由于4>0 继续加
第三遍 得到9,sum=4+9=13 13>7 max更新为13, 因为13>0 所以继续加
第四遍 得到-17 sum=13+(-17)=-4, max不更新,因为-4<0,所以开始新的子列,head往后移,sum清零。
以此类推,得到最大和为22,从第5位到第8位。
以下是代码部分:

#include<stdio.h>
int buf[100010];
int main()
{
    int n,max,head,tail,sum,head_max,tail_max,first=1;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        if(first)
            first=0;
        else
            printf("\n");//打印中间空行
        sum=0;
        max=-9999;//初始化使max最小,确保第一遍就能更新max的值为buf[0]
        head=head_max=tail_max=0;//初始化所有参数设在数组开头
        tail=-1;
        int b,t;
        scanf("%d",&t);
        for(b=0;b<t;b++)
        {
            scanf("%d",&buf[b]);
            tail++;
            if(sum+buf[b]>max)//及时更新max,子列开始标志位head_max,子列结束标志位tail_max
            {
                max=sum+buf[b];
                head_max=head;
                tail_max=tail;
            }
            if(sum+buf[b]<0)//如果之前的子列和小于0,则放弃前面的子列,重新开始新子列
            {
                head=b+1;
                sum=0;
            }
            else
            {
                sum+=buf[b]; //如果和非负,就继续加下去
            }
        }
        
        printf("Case %d:\n%d %d %d\n",i+1,max,head_max+1,tail_max+1);
    }
}
有什么错误,希望大家多多斧正!
有没讲清楚的地方大家也可以私信我,谢谢!
  
  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值