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. Spark思维导图之Spark SQL

  2. 使用Protobuf定义网络协议

    准备工具: 工具下载地址如下:https://github.com/protocolbuffers/protobuf/releases/tag/v3.6.1,主要使用到的文件有: protoc.exe ...

  3. servlet增删改查

    Servlet 使用Servlet进行增删改查 步骤 1.  导入以下几个jar包到WEB-INF下的lib包里,必要时build path,其中jstl.jar和standard.jar是使用jst ...

  4. 页面跳转、底部tabs切换页面

    1.页面跳转 import { NavController } from 'ionic-angular'; constructor(public navCtrl: NavController) { } ...

  5. 计算价格, java中浮点数精度丢失的解决方案

    计算价格, java中浮点数精度丢失的解决方案

  6. spring4 注入参数

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. 省电优化之WakeLock

    https://blog.csdn.net/qfanmingyiq/article/details/54587664 boolean onStopJob (JobParameters params) ...

  8. 20165221 Linux安装及命令入门学习

    安装过程 按照图文教程,进行操作,遇到如下问题. 1.安装ubuntu时从官网下载不成功. 最后在同学的帮助下,通过中文版网址入口进入,完成下载. 2.BIOS未恢复出厂设置,导致不能选择64-bit ...

  9. debian 9 安装 teamviewer 13

    1. 官网下载 https://www.teamviewer.com/zhcn/download/linux/ 2.安装 sudo dpkg -i teamviewer_13.0.5693_amd64 ...

  10. Linux今日疑问

    问题一:who -r没有反应 ========== 首先我查询了运行等级的含义. 0: 关机 1: 单用户 2: 无网络的多用户 3: 命令行模式 4: 未用 5: GUI(图形桌面 模式) 6 : ...