Max Sum

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 207989 Accepted Submission(s): 48681

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


本题用DP求最大子序列,用l和r标记下标

用一个b记录子序列大小,若为负,则对整个序列起减小作用,不取,

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std; int main()
{
int o,n,a[100005],b,mx,l,r,l1,r1;
while(~scanf("%d",&o))
{
int t=0;
int q=o;
while(o--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
b=0;
mx=-1001;
l=r=l1=r1=1; for(int i=1;i<=n;i++)
{
if(b<0)
{
l=i;
r=i;
b=a[i];
}
else
{
r=i;
b+=a[i];
}
if(mx<b)
{
l1=l;
r1=r;
mx=b;
}
}
printf("Case %d:\n",++t);
printf("%d %d %d\n",mx,l1,r1);
if(t!=q)
printf("\n");
}
}
return 0;
}

Max Sum—hdu1003(简单DP) 标签: dp 2016-05-05 20:51 92人阅读 评论(0)的更多相关文章

  1. 高质量C++C编程指南笔记 标签: c++笔记 2015-11-22 20:59 179人阅读 评论(0) 收藏

    1.  在多重循环中,如果有可能,应当将最长的循环放在最内层,最短的循环放在最外层,以减少 CPU 跨切循环层的次数. 2.  如果循环体内存在逻辑判断,并且循环次数很大,宜将逻辑判断移到循环体的外面 ...

  2. HDU1506(单调栈或者DP) 分类: 数据结构 2015-07-07 23:23 2人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. [J2EE规范]JDBC简单例子 标签: 数据库j2eejdbcjava 2017-06-29 10:55 353人阅读 评论(12)

    JDBC是什么? JDBC是java数据库连接(Java Database Connectivity),它是用于java编程语言和数据库之间的数据库无关连接的标准Java API,就是说,JDBC是用 ...

  4. 【UML】之简单概括 标签: uml图形 2014-11-09 11:24 1130人阅读 评论(24) 收藏

    29号开始看UML的视频,由于之前看视频总是一拖拖上半个月,所以这次打算速战速决,而且UML视频的讲解和内容并不算多,也比较容易懂,到后期更是花了很多时间来举例子巩固各种图的画法,所以这次花了11天初 ...

  5. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...

  6. save与Update的合并操作 标签: 关系映射 2017-07-13 15:11 7人阅读 评论(0) 收藏

    做save与update的方法合并操作时,判断条件是主体对象的ID是否存在. 但是当页面中,涉及到多个主体对象的关联对象时,情况变得复杂起来,特总结项目中的几点 一.页面中的VO对象属性可以分为三类: ...

  7. Pygame:编写一个小游戏 标签: pythonpygame游戏 2017-06-20 15:06 103人阅读 评论(0)

    大学最后的考试终于结束了,迎来了暑假和大四的漫长的"自由"假期.当然要自己好好"玩玩"了. 我最近在学习Python,本意是在机器学习深度学习上使用Python ...

  8. NYOJ-244 16进制的简单运算 AC 分类: NYOJ 2014-01-17 21:11 195人阅读 评论(0) 收藏

    #include<stdio.h> int main() { long x,y; char op; int t; scanf("%d ", &t); while ...

  9. 数字图像处理实验(总计23个)汇总 标签: 图像处理MATLAB 2017-05-31 10:30 175人阅读 评论(0)

    以下这些实验中的代码全部是我自己编写调试通过的,到此,最后进行一下汇总. 数字图像处理实验(1):PROJECT 02-01, Image Printing Program Based on Half ...

随机推荐

  1. ubuntu上mongodb的安装

    Ubuntu上安装MongoDB的完全步骤以及注意事项 本文我们详细介绍了Ubuntu上安装MongoDB的全部过程,希望本次的介绍能够对您有所帮助. AD: 2013大数据全球技术峰会课程PPT下载 ...

  2. 第一个android ijkplayer播放器

    创建一个ijkplayer的播放器项目,需要三步设置: 一.在activity_main.xml中添加播放器标签 <com.smallart.myapplication.media.IjkVid ...

  3. org.apache.commons.net.ftp

    org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object Java.lang.Object继承 org.apache. ...

  4. Tech 2 doesn’t system for H2 above 2007

    I purchased my Tech2 from obd2tool.com and it operates excellent. Can not definitely compare softwar ...

  5. c++文件中引用C代码

    下面提供一个比较完整的示例程序,一共有四个文件:main.cpp.test.c.test.h.test.hpp main.cpp #include "test.hpp" int m ...

  6. word2vec_文本相似度

    #提取关键词#关键词向量化#相似度计算 from jieba import analyseimport numpyimport gensim # 实现给出任意字符串,获取字符串中某字符的位置以及出现的 ...

  7. ajax 跨域请求没有带上cookie 解决办法

    公司项目前后端分离.. 前端全部html 静态页面.. 后端java 接口服务 由于前后端分离,出现跨域问题. 为了解决,我们使用jsonp 方式请求接口服务,暂时解决了跨域问题(使用jquery a ...

  8. Imageview 按比例适应屏幕大小

    DisplayMetrics dm = new DisplayMetrics();//取得窗口属性getWindowManager().getDefaultDisplay().getMetrics(d ...

  9. HACK字体安装

    参考:https://github.com/source-foundry/Hack Linux的 下载最新版本的Hack. 从存档中提取文件(.zip). 将字体文件复制到系统字体文件夹(通常/usr ...

  10. Linux CPU Hotplug CPU热插拔

    http://blog.chinaunix.net/uid-15007890-id-106930.html   CPU hotplug Support in Linux(tm) Kernel Linu ...