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. 

InputThe 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). 
OutputFor 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
题意:找出最大的连续字串和;
#include<iostream>
#include<map>
using namespace std;
int main()
{
int t,mark=,cnt=;
cin >> t;
while (t--)
{
if (cnt) cout << endl;
cnt = ;
mark++;
int temp = , frist, end;
int n,ko,sum=,max=-;
cin >> n;
for (int i = ; i < n; i++)
{
cin >> ko;
sum += ko;
if (sum > max)
{
max = sum; frist = temp; end = i + ;
}
if (sum < )
{
sum = ; temp = i + ;
}
}
cout << "Case " << mark << ":" <<endl<< max << " " << frist << " " << end << endl;
}
return ;
}

注意:因为要输出下标,必须灵活应用temp这个中间值,刚开始因为一直没仔细考虑下标的情况,导致负数情况下不能出正确答案;

通过temp的加入后,可以在大于max的条件满足下更改最后的下标,小于零的情况下可以更改temp,到累计大于max时,直接改frist;

(经典DP题)

Max Sum (dp)的更多相关文章

  1. HDU 1003 Max Sum(DP)

    点我看题目 题意 : 就是让你从一个数列中找连续的数字要求他们的和最大. 思路 : 往前加然后再判断一下就行. #include <iostream> #include<stdio. ...

  2. HDU 1003:Max Sum(DP,连续子段和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  3. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

  4. ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)

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

  5. UVA 10891 Game of Sum(DP)

    This is a two player game. Initially there are n integer numbers in an array and players A and B get ...

  6. HDU-1003:Max Sum(优化)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. hdu 1003 Max Sum (动态规划)

    转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...

  8. URAL 1146 Maximum Sum(DP)

    Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...

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

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

随机推荐

  1. D - The Lucky Week ZOJ - 3939 (思维)

    题目链接: D - The Lucky Week  ZOJ - 3939 题目大意:幸运的星期指,星期一为每个月的1 or 11 or 21号.给出第一个幸运星期的时间,问从当前的日起开始.第n个的日 ...

  2. Tip: JSP开发模式

    SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. JSP+JavaBean模式适合开发业务 ...

  3. package-lock.json和package.json区别

    package.json里面定义的是版本范围(比如^1.0.0),具体跑npm install的时候安的什么版本,要解析后才能决定,这里面定义的依赖关系树,可以称之为逻辑树(logical tree) ...

  4. 线程变量---ThreadLocal类

    用处:保存线程的独立变量.对一个线程类(继承自Thread) 思想:如果一个资源会引起线程竞争,那就为每一个线程配置一个资源.相比于synchronized是一种空间换时间的策略 当使用ThreadL ...

  5. Intel Xeon E5-2620 v4参数

    基本参数 CPU系列 Xeon E5 v4系列 制作工艺 14纳米 核心代号 Broadwell 性能参数 核心数量 八核心 线程数量 十六线程 CPU主频 2.1GHz 动态加速频率 3GHz L3 ...

  6. Des加密解密算法java实现

    package tech.fullink.eaglehorn.utils; import javax.crypto.Cipher; import javax.crypto.SecretKey; imp ...

  7. MySQL:Your password has expired. To log in you must change it using a client that supports expired passwords

    MySQL:V5.6.37 安装后发现没远程权限,为了方便,就直接把hostname@root修改为%@root,密码修改为和localhost@root一样 然后尴尬的事情发生了,本地登陆正常,远程 ...

  8. 关于 DELPHI DATASNAP 的文章集

    关于 DELPHI  DATASNAP 的文章集: 1.墨者工作室  DataSnap基础 https://wenku.baidu.com/view/78715605cc1755270722088b. ...

  9. Linux将rm命令设置为回收站【转】

    一个方案就是重定向 rm 命令以嫁接为 mv 命令,相当于给 Linux 系统定制了一个回收站. 实现方式如下: ### 重定义rm命令 ### # 定义回收站目录 trash_path='~/.tr ...

  10. python3+selenium入门08-鼠标事件

    使用click()可以模拟鼠标的左键点击事件,现在的web页面中有很多其他的鼠标交互方式,比如鼠标右击.双击.悬停.鼠标拖放等功能.在WebDriver中,将这些关于鼠标操作的方法封装在ActionC ...