HDU 1003 Max Sum

原创 2013年12月02日 21:56:28
#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-1003 Max Sum (线性dp 最大连续和)

线性dp,最大连续和。题意:100000个数,求最大连续和并且输出方案,要求输出的区间两端点最小.。...
  • hmc0411
  • hmc0411
  • 2017年11月12日 20:24
  • 130

HDU 1003 Max Sum 最大连续和 分治法

题意:给出一个数组,求出最大连续和。 思路:这里我们用分治法来求解。和其他的数组上的分治一样,我们将数组A进行二等分为B,C;             最大连续和就有有三种可能:1.位于数组B中。...

HDU 1003 Max Sum (经典dp入门题)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su...

hdu 1003 Max Sum (动态规划)

转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Ja...

HDU-1003 Max Sum (dp)

题目: Given a sequence a[1],a[2],a[3]……a[n], your job is to calculate the max sum of a sub-sequence....
  • robin_8
  • robin_8
  • 2017年01月11日 20:10
  • 75

[背包问题][第三阶段-初见dp][HDU-1003]Max Sum

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s...

HDU1003 Max Sum(动态规划,最大子序列和)

题目: Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To...

HDU-1003-Max Sum(DP)

Max SumTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su...

hdu1003: Max Sum

http://acm.hdu.edu.cn/showproblem.php?pid=1003 经典动态规划

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 1003 Max Sum
举报原因:
原因补充:

(最多只允许输入30个字)