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. ffmpeg-- audio decoder

    测试代码来源于:http://ffmpeg.org/doxygen/trunk/decode_audio_8c-example.html /* * Copyright (c) 2001 Fabrice ...

  2. 【C语言】数组名作函数参数,完成数据的升序排列

    #include<stdio.h> void sort(int x[],int n); int main() { ] = { ,,,,,,,,, },i; sort(arr, ); pri ...

  3. 每天进步一点点------下载Microblaze程序到Flash

    第一步 生成下载文件(bit文件) 选择之前的工作目录,打开SDK.点击Program FPGA图标. 将bootloop项改为Hello_World.elf.点击Program.此时可以不用连接下载 ...

  4. Box/坐标/方向/Row

    1.Box, 我们在做design planning的第一步就是确定floorplan的box,也就是设计的区域.这个区域可以划分为三个边界,如下图所示: 上图中,按对应的颜色框框可以分为:Die B ...

  5. Object的rest和spread方法

    //将多个对象合并到一个对象里 const input = { a: 1, b: 2 } const test = { d: 5 } const output = { ...input, ...tes ...

  6. 记一次if控制器的使用

    1.添加if控制器 2.输入判断条件:常见的就是某个变量是不是等于某个值 3.或者用函数助手中的函数 每个版本jmeter函数助手的入口不同,我的直接在菜单上: 选择__jexl3,输入判断条件,点击 ...

  7. buuctf 二维码

    首先下载文件 然后用解压工具解压之后 发现是一个二维码 扫描二维码 并没有拿到 flag 然后将图片拖进 hxd中搜索PK发现有一个压缩包  将压缩包提取出来 暴力破解 然后得到密码 然后解压 然后得 ...

  8. Codeforces Round #577 (Div. 2) 题解

    比赛链接:https://codeforc.es/contest/1201 A. Important Exam 题意:有\(n\)个人,每个人给出\(m\)个答案,每个答案都有一个分值\(a_i\), ...

  9. java篇 之 变量存放位置

    一:在方法中声明的变量,即该变量是局部变量,每当程序调用方法时,系统都会为该方法建立一个方法栈,其所在方法中声明的变量就放在方法栈中,当方法结束系统会释放方法栈,其对应在该方法中声明的变量随着栈的销毁 ...

  10. LoadLibrary与LoadLibraryEx

    参考了这个博客:https://blog.csdn.net/cuglifangzheng/article/details/50580279 以及这个博客:https://blog.csdn.net/z ...