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

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 135262    Accepted Submission(s): 31311

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

解题思路:

当全部数都为负数时,最大子段和为0.

int MaxSum(int num[],int n)
{
    int sum=0,b=0;
    int i;
    for (i=1;i<=n;i++)
    {
        if(b>0)
            b+=num[i];
        else
            b=num[i];
        if(b>sum)
            sum=b;
    }
    return sum;
}

仅仅求最大和,没有保存位置。

保存位置:start为起 end为末

       int start=1,end=1,s=1,e=1;
       int sum=0,max=num[1];//不能让max=0
        for(int i=1;i<=n;i++)
        {
            e=i;
            sum=sum+num[i];
            if(max<sum)
            {
                max=sum;
                start=s;
                end=e;
            }
            if(sum<0)
            {
                s=i+1;
                sum=0;
            }
        }

本题须要保存起始位置。以下代码假设全是负数,输出最小的那个位置

代码:

#include <iostream>
using namespace std;
const int maxn=100002;
int num[maxn];
int n;
int main()
{
    int t;cin>>t;int c=1;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>num[i];
        int start=1,end=1,s=1,e=1;//这里start end一定要赋值为1
        int sum=0,max=num[1];//不能让max=0
        for(int i=1;i<=n;i++)
        {
            e=i;
            sum=sum+num[i];
            if(max<sum)
            {
                max=sum;
                start=s;
                end=e;
            }
            if(sum<0)
            {
                s=i+1;
                sum=0;
            }
        }
        cout<<"Case "<<c++<<":"<<endl;
        cout<<max<<" "<<start<<" "<<end<<endl;
        if(t)
            cout<<endl;
    }
    return 0;
}

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

时间: 2024-08-09 06:32:51

[ACM] hdu 1003 Max Sum(最大子段和模型)的相关文章

[ACM] hdu 3415 Max Sum of Max-K-sub-sequence (单调队列)

高一时,学校组织去韶山游玩,我没去,这次趁着五一,总算去了我心心念念的韶山.其实我知道所有的景点都是差不多的,可是因为电视剧<恰同学少年>,让我对毛泽东有了进一层的了解,所以,我一直都想去看看. 有两个同学一男一女是我理想的旅友,可是女生不想去,而男士回家了.所以,我独自一人去了. 准备工作:一小包饼干,一小包山楂片,两个苹果,一瓶水,帽子(防晒),墨镜(装酷) 早晨5:30起床了,洗漱完毕,吃完早餐,赶到公交车站牌那里,才6点过几分.公交车6:31才到,等了近半个小时(公交车上明明说是6:0

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

HDU 1003 Max Sum(dp,最大连续子序列和)

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

HDU 1003 Max Sum (动规)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 141547    Accepted Submission(s): 32929 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU 1003 Max Sum【动态规划求最大子序列和详解 】

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 59365 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

HDU 1003 Max Sum(最大子列和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 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

hdu 1003 Max Sum 简单DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 转移方程:dp[i]=max(dp[i-1]+a[i],a[i]) 虽然是dp 但不用真的申请一个dp数组 #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <cmath> #include <cstring> #in

HDU 1003——Max Sum(动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:历遍所有数字,找出最大字段和. 解题思路: t和n:记录循环次数和每一段有多少个数字 temp,now,max:temp存放临时读取的变量,now代表现在和,max代表当前最大和,如果前面相加后是负数,而后一位是正数,则更新起点位置. 代码如下: 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 in

HDU - 1003 - Max Sum &amp;&amp; POJ - 1050 - To the Max (经典DP问题)

题目传送:HDU - 1003 思路:最大子序列和 dp[i]= a[i]   (dp[i-1]<0) dp[i]= dp[i-1]+a[i]   (dp[i-1]>=0) AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include