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
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.
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).
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.
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
#include <iostream>
using namespace std;
int main()
{
int j,i,k,n,m,t;
int a[];
scanf("%d",&t);
for (j=;j<=t;j++)
{
scanf("%d",&n);
for (i=;i<n;i++)
{
scanf("%d",&a[i]);
}
int sum=,maxsum=-,first =, last = , temp = ;
for (i=;i<n;i++)
{
sum += a[i];
if (sum > maxsum)
{
maxsum = sum;first = temp;last = i+;
}
if (sum < )
{
sum = ;temp = i+;
}
} printf("Case %d:\n%d %d %d\n",j,maxsum,first,last);
if (j!=t)
{
printf("\n");
}
} return ;
}
#include <iostream>
using namespace std;
int main()
{
int j,i,k,n,m,t;
int a; //不需要数组,只需要一个输入变量
scanf("%d",&t);
for (j=;j<=t;j++)
{
scanf("%d",&n);
int sum=,maxsum=-,first =, last = , temp = ;
for (i=;i<n;i++)
{
scanf("%d",&a);
sum += a;
if (sum > maxsum)
{
maxsum = sum;first = temp;last = i+;
}
if (sum < )
{
sum = ;temp = i+;
}
}
//注意格式,我就因为将冒号写到了数的前边而wrong answer,郁闷半天才发现……
printf("Case %d:\n%d %d %d\n",j,maxsum,first,last);
if (j!=t)
{
printf("\n");
}
} return ;
}
HDU 1003 Max Sum【动态规划求最大子序列和详解 】的更多相关文章
- 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 (动态规划 最大区间和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu 1003 Max Sum(动态规划)
解题思路: 本题在给定的集合中找到最大的子集合[子集合:集合的元素的总和,是所有子集合中的最大解.] 结果输出: 最大的子集合的所有元素的和,子集合在集合中的范围区间. 依次对元素相加,存到一个 su ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- 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 (动态规划)
转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...
- HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1003 Max Sum 求区间最大值 (尺取法)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
随机推荐
- 二叉树Binary_Tree(1):二叉树及其数组实现
定义 二叉树: 二叉树是一种特殊的树.二叉树的特点是每个结点最多有两个儿子,左边的叫做左儿子,右边的叫做右儿子,或者说每个结点最多有两棵子树.更加严格的递归定义是:二叉树要么为空,要么由根结点.左子树 ...
- Swift 开源项目练习应用
小的View.动画实现练习 拥有着苹果先天生态优势的Swift自发布以来,各种优秀的开源项目便层出不穷.本文作者站在个人的角度,将2014年Swift开源项目做了一个甄别.筛选,从工具.存储.网络.界 ...
- 记一次生产环境Nginx日志骤增的问题排查过程
摘要:众所周知,Nginx是目前最流行的Web Server之一,也广泛应用于负载均衡.反向代理等服务,但使用过程中可能因为对Nginx工作原理.变量含义理解错误,或是参数配置不当导致Nginx工作异 ...
- (通用)深度学习环境搭建:tensorflow安装教程及常见错误解决
区别于其他入门教程的"手把手式",本文更强调"因"而非"果".我之所以加上"通用"字样,是因为在你了解了这个开发环境之后 ...
- solr 的 field, copyfield ,dynamic field
Field: Field就是一个字段,定义一个Field很简单: <field name="price" type="sfloat" indexed=&q ...
- 最优化算法:BFGS算法全称和L-BFGS算法全称
在最优化算法研究中按时间先后顺序出现了许多算法包括如下几种,这里介绍下他们的全称和英文名称: 1.最速下降法(Gradient descent) 2.牛顿法(Newton method) 3. 共轭梯 ...
- Web程序员必备的CSS工具
对于web开发来说,CSS是最有效的美化页面.设置页面布局的技术.但问题是,CSS是一种标记性语言,语法结构非常的松散.不严谨.WEB程序员会经常发现自己的或别人的CSS文件里有大量的冗余代码或错误或 ...
- git常用使用命令
Git远程操作详解(from 阮一峰) git clone.git remote.git fetch.git pull.git push git拉取远程分支并创建本地分支 git中文学习文档(详细) ...
- 【数论】洛谷P1372又是毕业季
题目背景 "叮铃铃铃",随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业 ...
- Python多线程编程(第二篇)
一.Python中的上下文管理器(contextlib模块) 上下文管理器的任务是:代码块执行前准备,代码块执行后收拾 1.如何使用上下文管理器: 如何打开一个文件,并写入"hello wo ...