HDU 1003 Max Sum

#include <iostream>
#include <cstdio>
#define maxn 100010
using namespace std;
int T,N,a[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    int g=1,flag;
    scanf("%d",&T);
    while(T--)
    {
        int begin,end,t;
        __int64 sum,maxsum;
        begin=end=0;
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            scanf("%d",&a[i]);
        }
        sum=maxsum=a[0],flag=0,t=0;
        for(int i=1;i<N;i++)
        {
            if(sum<0)
            {
                sum=a[i];
                if(sum>maxsum)
                {
                    maxsum=sum;
                    end=i;
                }
                t=0;
                continue;
            }
            if(sum+a[i]<a[i])
            sum=a[i];
            else
            {
                sum+=a[i];
                t++;
            }
            if(sum>maxsum)
            {
                maxsum=sum;
                end=i;
                flag=t;
            }
        }
        begin=end-flag;
        printf("Case %d:\n%I64d %d %d\n",g,maxsum,begin+1,end+1);
        g++;
        if(T)
        printf("\n");
    }
    return 0;
}

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.
 
推荐测试数据:
2
5 -3 -2 -1 -2 -3 ———— -1 3 3
 -1 -2 -3 -4 -5 -6—————-1 1 1
阅读更多
个人分类: 杭州电子科技
上一篇hdu 1002 A + B Problem II
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭