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(最大子段和模型)的更多相关文章

  1. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  2. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  3. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

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

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

  5. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  6. hdu 1003 Max Sum (动态规划)

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

  7. HDU - 1003 Max Sum 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意 给出一个序列 要求找出一个和最大的子序列 思路 O(N)的做法 但是要标记 子序列的头部位 ...

  8. HDU 1003 Max Sum (动规)

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

  9. hdu 1003 Max sum(简单DP)

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

随机推荐

  1. VC++学习之一

    对于编程语言,我一直认为它只是一种工具,就像锤子,斧头一样,每种语言都用自己比较适用的地方,用的时候拿来就可以了.这种思想让我对语言没有做过很仔细的学习,虽然频繁使用过C,C++,java,C#,De ...

  2. Android 自定义PopupWindow动画效果

    public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...

  3. URL组成介绍

    1.2. HTTP request ----------------- First, let's consider this HTTP request : Line Contents number 1 ...

  4. C++数据结构之最小生成树

    最小生成树是图的一部分,一般求最小生成树用Prim算法和Kruskal算法. 对于Prim算法,思想是:在访问过的顶点和未访问的顶点之间选择权值最小的边.Prim算法是基于顶点的操作,适合于顶点较少, ...

  5. Linux,Unix各种版本的操作系统在线安装软件命令

    摘自:http://blog.csdn.net/zjg555543/article/details/8278266 linux和unix,各个版本的操作系统都有自己的软件安装方式,最方便的莫过于在线安 ...

  6. Java面向对象面试案例

  7. 7-05. 魔法优惠券(25) (数学 ZJU_PAT)

    题目链接:http://www.patest.cn/contests/ds/7-05 在火星上有个魔法商店,提供魔法优惠券.每一个优惠劵上印有一个整数面值K,表示若你在购买某商品时使用这张优惠劵.能够 ...

  8. edit distance leetcode

    这样的字符转换的dp挺经典的, 若word1[i+1]==word2[j+1] dp[i+1][j+1] = dp[i][j]:否则,dp[i+1][j+1] = dp[i][j] + 1.(替换原则 ...

  9. 在SSH框架中增加SiteMesh的支持

    1)引入jar包,如下两个jar包需要导入到系统的lib文件夹中: sitemesh-2.4.jar struts2-sitemesh-plugin-2.2.1.1.jar 2)修改web.xml增加 ...

  10. 查看当前支持的MySQL字符集的命令

    查看不同的MySQL字符集有不同的方法,下面介绍的命令用于查看当前支持的MySQL字符集,希望对您学习MySQL字符集能有所帮助. mysql> show char set; +-------- ...