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 ...
随机推荐
- Hibernate学习---用Session实现CURD
我们使用Hibernate的目的是什么?对数据库进行操作,所有接下来我们就用Hibernate来进行CURD. 前边我们已经分析过了Configuration,SessionFactory和Sessi ...
- 基于CDH 5.9.1 搭建 Hive on Spark 及相关配置和调优
Hive默认使用的计算框架是MapReduce,在我们使用Hive的时候通过写SQL语句,Hive会自动将SQL语句转化成MapReduce作业去执行,但是MapReduce的执行速度远差与Spark ...
- ABP前端使用阿里云angular2 UI框架NG-ZORRO分享
一.前言 前段时间写博客分享和介绍了阿里云的UI框架NG-ZORRO(博客请查看:http://www.cnblogs.com/donaldtdz/p/7892960.html),结合近段时间对.Ne ...
- [数据结构]C语言二叉树的实现
树和图是数据结构中比较麻烦的东西,里面涉及的概念比较多,也最有用, 就比如一般树广泛应用于人工智能的博弈上,而基于图的广度优先和深度优先搜索也广泛应用于人工智能寻路上面 首先我们要把树进行分类: &g ...
- splay小结—植树结
我要把高级数据结构当爸爸了... ...弱到跪烂了. splay,二叉搜索树的一种,具有稳定变形功能. 二叉搜索树:对于一个节点,都只有不超过2个孩子.其左子树内的点的权值都比这个点小,右子树的点的权 ...
- 3.sass的数据类型与函数
数据类型 在sass里有数字.字符串.列表.颜色等类型 在cmd里 输入 sass -i 就会进入到交互模式,输入的计算可以马上得到结果 type-of()可以用来得到数据类型,如: type-of( ...
- 前端MVC Vue2学习总结(五)——表单输入绑定、组件
一.表单输入绑定 1.1.基础用法 你可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.尽管有些神奇,但 v-model 本质上不过是语法糖, ...
- asp.net core webapi 服务端配置跨域
在前后端分离开发中服务端仅仅只为前端提供api接口,并且前后端往往单独部署,此时就会出现浏览器跨域问题.asp.net core提供了简单优雅的解决方案. 在startup文件的Configure添加 ...
- 利用USearch去除嵌合体(chimeras)
嵌合体序列指在pcr过程中,两条不同的序列产生杂交扩增的序列,属于人工污染,在ITS和16S分析中,应该首先去除,USearch提供去除嵌合体的功能 usearch -uchime_ref reads ...
- Gradle、Gradle Wrapper与Android Plugin for Gradle
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...