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. Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)

    一.实现功能 1.服务器端选择待发送的文件,可以是多个 2.开启服务器,支持多客户端接入,能够实时显示每个客户端接入状态 3.等待所有客户端都处于已连接状态时,依次发送文件集给每个客户端,显示每个客户 ...

  2. 网易云课堂_C++开发入门到精通_章节3: 类、对象和封装

    课时12构造函数与析构函数-2 构造函数 构造函数可以有多个 构造函数可以重载 构造函数用于隐式类型转换 class Student { public: explicit Student(int ss ...

  3. C#字典Dictionary排序(顺序、倒序)

    这里是针对.NET版本过低的排序方式,没怎么用过,记录一下: 一.创建字典Dictionary 对象 假如 Dictionary 中保存的是一个网站页面流量,key 是网页名称,值value对应的是网 ...

  4. 24点游戏&&速算24点(dfs)

    24点游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...

  5. SpringNote01.基于SpringMVC-Hibernate的Blog系统

    最近,在学习Spring,做这样一个简单的blog系统,主要是让自己动手练习使用Spring,熟练的使用才干进一步的深入学习.该项目使用Maven构建,使用git进行代码管理,通过这样一个小项目,熟悉 ...

  6. 好用的DNS服务器推荐

    DNS在平时上网中扮演重要角色,如果不注意DNS的话,可能会导致网速慢.弹窗广告.网址打不开.打开不是自己想要的网站.淘宝客劫持等一系列问题.针对DNS的问题,网络上也有各种DNS平台供用户选择.这里 ...

  7. 【c语言】调整数组使奇数所有都位于偶数前面

    // 调整数组使奇数全部都位于偶数前面 // 输入一个整数数组,实现一个函数,来调整该数组中数字的顺序使得数组中全部的奇数位于数组的前半部分, // 全部偶数位于数组的后半部分. #include & ...

  8. 【最大流,二分图匹配】【hdu2063】【过山车】

    题意:裸的求二分图匹配 建立一个源点 连向一边所有的点 容量为1; 另外一边点都连向汇点  容量为1; 二分图的边容量也为1 源点汇点求一遍最大流即可 #include <cstdio> ...

  9. zoj1027 Human Gene Functions

    一道动态规划,两个串进行匹配,不同字母匹配的值不一样,也可以和空格匹配(空格不能与空格匹配),求最大的匹配值. 数据很弱,每个串都在100以内. 定义dp[i][j]为第一个串前i个数和第二个串前j个 ...

  10. Android_神奇的android:clipChildren属性

    正文 一.效果图 看到这个图时你可以先想想如果是你,你怎么实现这个效果.马上想到用RelativeLayout?NO,NO,NO,,, 二.实现代码 <?xml version="1. ...