HDU 1003 MAX SUM

原创 2016年08月28日 19:52:37

Max Sum

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


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
 

Recommend
We have carefully selected several similar problems for you:  1176 1087 1069 1058 1203 
 


题目大意:给定一个序列,求最大连续子序列之和。输入n和n个数字,每个案例输出最大连续子序列之和,和这个序列的起点和终点。

题目思路:n为100000,先想到dp,dp时间复杂度为n,所以不会超时。


求最大连续子序列之和模板:

if(dp[i-1]>0)
  dp[i]=dp[i-1]+a[i];
else
  dp[i]=a[i];

然后我们考虑起点和终点:当dp[i]=a[i]时,这时是一个新序列的开始,所以更新起点和终点。当dp[i]=dp[i-1]+a[i]时,只需要更新终点即可。


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#define N 100010
using namespace std;

int a[N],dp[N];

int main()
{
    int T,t=1;
    scanf("%d",&T);
    while(T--)
    {
        int n,s,e,i,e1,s1;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
           scanf("%d",&a[i]);

        memset(dp,0,sizeof(dp));
        dp[0]=-1001;           //因为序列的最小值为-1000,所以为了确保不会取到dp[0],就将dp[0]设为-1001
        int max1=dp[0];
        for(i=1;i<=n;i++)
        {
            if(dp[i-1]>=0)          //延续就序列,更新终点
            {
                dp[i]=dp[i-1]+a[i];
                e1=i;

            }
            else
            {
                dp[i]=a[i];           //新序列,更新起点和终点
              {
                        s1=i;
                        e1=i;
              }
            }
            if(dp[i]>max1)           //更新最大值和结果
            {
                max1=dp[i];
                s=s1;
                e=e1;
            }
}
        
        printf("Case %d:\n",t);
        printf("%d %d %d\n",max1,s,e);
        if(T!=0)
            printf("\n");
        t++;
    }
    return 0;
}

妈妈咪呀,我的表情包出了故障,不见了,不见了,好可怕,那就只有我百度来的表情包了,可是我qq盗来的表情包更可爱啊,啊啊啊,怎么办啊……



版权声明:本文为博主原创文章,未经博主允许不得转载。

【HDU1003】【Max Sum】【2种代码】

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

HDU1003 Max Sum【最大子段和+DP】

问题链接:HDU1003 Max Sum。 问题简述:参见上述链接。 问题分析:计算最大子段和问题,是一个经典的动态规划问题。 程序说明:这个算法可以说是最为快速简洁的算法,其计算复杂度为O(n)...

HDU 1003 Max Sum 最大连续子序列和

Max SumTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Problem De...
  • FTQOOO
  • FTQOOO
  • 2015年09月10日 21:33
  • 209

HDU 1003.Max Sum【最大连续子序列和】【8月14】

Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calcul...

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...
  • AXuan_K
  • AXuan_K
  • 2014年08月04日 15:58
  • 664

hdu 1003 Max Sum(最大连续子序列和) (学了一下分治)

都不知道以前刷杭电是怎么做的最大连续子序列和,

[ACM] hdu 1003 Max Sum(最大子段和模型)

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

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...

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

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

hdu1003 Max Sum 最大子列和问题

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 1003 MAX SUM
举报原因:
原因补充:

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