[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
1000).
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 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Case 1:
14 1 4 Case 2:
7 1 6
解题思路:
当全部数都为负数时,最大子段和为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(最大子段和模型)的更多相关文章
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- hdu 1003 Max Sum (DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- hdu 1003 Max Sum (动态规划)
转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...
- HDU - 1003 Max Sum 【DP】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意 给出一个序列 要求找出一个和最大的子序列 思路 O(N)的做法 但是要标记 子序列的头部位 ...
- 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(简单DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
随机推荐
- cf459B Pashmak and Flowers
B. Pashmak and Flowers time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 后缀数组da3模板
在做poj2406的时候...按论文给的rmq模板会超内存...然后网上找了http://blog.csdn.net/libin56842/article/details/46310425这位大爷的d ...
- 通过YAJL获取json中的值
这里主要是举例说明一下假设通过yajl获取json中的值. 对于array和object来说,获取的方式略有不同,详细能够參考以下的代码. 我仅仅是从网上搜集信息.知道有这么一种方法.假设还有别的方法 ...
- How to recover a skipped tablespace after an incomplete recovery with resetlogs? [ID 1561645.1]
n this Document Goal Solution This document is being delivered to you via Oracle Support's Rapid ...
- C# 8 函数 调用 常用类 时间 日期型
函数:能够独立完成某个功能的模块. 好处:1.结构更清析(编写.维护方便 ).2.代码重用.3.分工开发. 四要素:名称,输入(参数),输出(返回的类型),加工(函数体) 语法: 返回类型 函数名(参 ...
- Powerdesigner逆向工程从sql server数据库生成pdm (完整版)
第一步:打开"控制面板"中的"管理工具" 第二步:点击"管理工具"然后双击"数据源(odbc)" 第三步:打开之后,点击 ...
- 本地网址连不上远程mysql问题
问题:host 'XXX.XXX.XXX.XXX'is not allowed to connect to this MySQL server 解决办法: 进入远程mysql #mysql -u ro ...
- JS如何将UTC格式时间转本地格式
Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, //month & ...
- 转载 java枚举类型enum的使用 (原文地址:http://blog.csdn.net/wgw335363240/article/details/6359614)
java枚举类型enum的使用 最近跟同事讨论问题的时候,突然同事提到我们为什么java中定义的常量值不采用enmu枚举类型,而采用public final static 类型来定义呢?以前我们都是采 ...
- cordova开发中遇到的一些坑
# 外域的JS被禁止,修改meta标签 <meta http-equiv="Content-Security-Policy" content="default-sr ...