Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 252815    Accepted Submission(s): 59950

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

 
题目意思很简单,就是在一个数组中找最大子串(这个是要连续的,之前那个是不连续的要求递增的)。
然后题目的精髓就在于: 一开始把first指针指向第一个位置,然后用一个temp变量也指向第一个位置,然后当有sum>maxsum的时候,就执行first=temp,保证了最大子串在后面依然可以被找到。计算子串的sum<0时就表示要重新找子串了,temp=j+1,sum=0,当找到下一个>0的数的时候,(因为有temp的实时更新)first指向该数,重新开始计算sum。
 
然后 就要注意下输出格式的问题。具体参考下第一次的AC代码

杭电1003 最大子串(第二次AC) 当作DP的训练吧的更多相关文章

  1. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

  2. 杭电1003 Max Sum 【连续子序列求最大和】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...

  3. HDU 5745 La Vie en rose (DP||模拟) 2016杭电多校联合第二场

    题目:传送门. 这是一道阅读理解题,正解是DP,实际上模拟就能做.pij+1 指的是 (pij)+1不是 pi(j+1),判断能否交换输出即可. #include <iostream> # ...

  4. HDU 5744 Keep On Movin (贪心) 2016杭电多校联合第二场

    题目:传送门. 如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了. #include <iostream ...

  5. HDU 5742 It's All In The Mind (贪心) 2016杭电多校联合第二场

    题目:传送门. 题意:求题目中的公式的最大值,且满足题目中的三个条件. 题解:前两个数越大越好. #include <iostream> #include <algorithm> ...

  6. HDU 5734 Acperience (公式推导) 2016杭电多校联合第二场

    题目:传送门. #include <iostream> #include <algorithm> #include <cstdio> #include <cs ...

  7. 杭电1003 MAX SUN

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

  8. 杭电oj 2037 今年暑假不AC

    Tips:贪心算法的典型应用,可以按照节目结束时间由小到大排序,(至于结束时间相同的,有些人说按开始时间早的排序,不过个人认为不必处理,因为结束时间一样,两个之中要么都没有,要么必有一个)然后再依次进 ...

  9. 杭电 1003 Max Sum (动态规划)

    参考:https://www.cnblogs.com/yexiaozi/p/5749338.html #include <iostream> #include <cstdio> ...

随机推荐

  1. webpack配置的说明

    {devtool: 'source-map',//要启用source-map需加上此配置项,同时css或less的loader要加上参数?sourceMap,js的loader不用加 entry: e ...

  2. Jarvis OJ - DD-Hello -Writeup

    Jarvis OJ - DD-Hello -Writeup 转载请注明出处http://www.cnblogs.com/WangAoBo/p/7239216.html 题目: 分析: 第一次做这道题时 ...

  3. ASP.NET的 Razor引擎和JavaScript是一种什么关系

    Razor能做的JS大部分能做,不是全部.比如说,如果你用了Entity Frame一类的ORM的时候,Razor可以直接绑定数据库数据,但JS就不可能直接访问数据库——必须通过其他web servi ...

  4. Java 动态代理实现

    1.依赖 java.lang.reflect.Proxy - 提供了静态方法去创建动态代理类的实例: Interface InvocationHandler - 一个代理实例调用处理程序实现的接口 2 ...

  5. windows CMakeLists.txt

    在windows下写好CMakeLists.txt,然后配合cmake-gui使用. CMakeLists.txt写的不够好,后期优化,以下仅供参考: # set(OpenCV_DIR D:/Prog ...

  6. centos 6.10 安装mysql 5.7.27

    操作系统Centos 6.10 64位 Mysql 版本 5.7.27 , 从官网下载 该教程是Mysql shell安装脚本,脚本运行结束后需要重置密码,以及必要的授权操作等 该教程对外端口设置为5 ...

  7. 【Python】使用socketserver建立一个异步TCP服务器

    概述 这篇文章是讲解如何使用socketserver建立一个异步TCP服务器,其中Python版本为3.5.1. socketserver主要的类 socketserver模块中的类主要有以下几个:1 ...

  8. jmeter-BeanShell PreProcessor的使用

    BeanShell简介 BeanShell是一个小型嵌入式Java源代码解释器,具有对象脚本语言特性,能够动态地执行标准JAVA语法.在BeanShell中,我们可以使用java语言自定义函数来处理特 ...

  9. MBA 报考

      1. 作者:MBA薛老师链接:https://www.zhihu.com/question/277811289/answer/397083199来源:知乎著作权归作者所有.商业转载请联系作者获得授 ...

  10. IDF-CTF-简单的Elf逆向Writeup

    ElfCrackMe1 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...