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
 

这是一道求最大子序列和的题。

思路就是考虑到对于S(i...k) + S(k+1...j) = S(i...j),如果S(i...k)小于0,自然考虑S(k+1...j)这段和;反之,考虑S(i...j)。

于是从1到n,判断当前的S(i...k)是否小于0,大于0则保留,否则舍去。

考虑到可能整个过程可能S(i...k)一直小于0,所以即使小于0,也要保留当前值now,将其与ans比较。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector> using namespace std; int n;
int ans, from, to; void Work()
{
from = -1;
to = -1;
int k, now, u = -1, v = -1;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d", &k);
if (u == -1 || now < 0 || now+k < 0)
{
u = v = i;
now = k;
}
else
{
v = i;
now = now+k;
}
if (from == -1 || now > ans)
{
ans = now;
from = u;
to = v;
}
}
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = 1; times <= T; ++times)
{
Work();
if (times != 1)
printf("\n");
printf("Case %d:\n", times);
printf("%d %d %d\n", ans, from, to);
}
return 0;
}

ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)的更多相关文章

  1. ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  2. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  3. 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾

    2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...

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

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

  5. ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)

    Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...

  6. hdu1003 Max Sum(经典dp )

      A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descr ...

  7. ACM学习历程—HDU 1059 Dividing(dp && 多重背包)

    Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...

  8. Max Sum(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  9. HDU1003 Max Sum(求最大字段和)

    事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...

随机推荐

  1. 转:DDR中端接技术基本概念

    DDR中端接技术基本概念  版权声明:转载请注明出处:http://blog.csdn.net/lg2lh https://blog.csdn.net/lg2lh/article/details/90 ...

  2. Struts2学习九----------处理结果类型(input)

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2处理结果类型 - SUCCESS:Action正确的执行完成,返回相应的视图,success是name属性的默认值 - ERROR:表示 ...

  3. Android 开发资源

    0x00 Android 入门资料 a. Android 学习之路 b.<第一行代码> 介绍及购买链接:http://blog.csdn.net/guolin_blog/article/d ...

  4. 应对ie双外边距,不使用hack

    1.在浮动元素内层加一层div 2.使用不浮动的内层外边距来定义距离 ie在浮动时,并且使用外边距,会产生双倍外边距.

  5. caffe2--------ImportError: No module named past.builtins

    whale@sea:~/anaconda2/lib/python2.7/site-packages$ python Python 2.7.14 |Anaconda custom (64-bit)| ( ...

  6. 【设计模式】C++单例模式的几种写法——Java自动加载内部类对象,C++怎么破?

    单例模式是最简单的设计模式,就让我像玩简单的游戏一样写下去吧. v1: 简单模式 和这个版本有过一面之缘,但不敢苟同. class Singleton { private: Singleton() { ...

  7. JavaWeb学习总结第二篇--第一个JavaWeb程序

    JavaWeb学习总结第二篇—第一个JavaWeb程序 最近我在学院工作室学习并加入到研究生的项目中,在学长学姐的带领下,进入项目实践中,为该项目实现一个框架(用已有框架进行改写).于是我在这里记录下 ...

  8. 【C语言】一句printf代码——{ a[0] ? 0[a] }

    这是前段时间做的http://fun.coolshell.cn/中的一道题,很有意思,涉及的其实是C的基础,不过当时第一次看见这行代码确实把我弄懵了: printf(&unix["\ ...

  9. 一步一步学ios UITextView(多行文本框)控件的用法详解(五5.8)

    本文转载至 http://wuchaorang.2008.blog.163.com/blog/static/48891852201232014813990/     1.创建并初始化 创建UIText ...

  10. 计算机网络--DNS

    1.域名系统DNS(domain name system)是因特网使用的命名系统,用来把便于人们时用的机器名字转换为IP地址.因特网的域名系统DNS被设计成一个联机分布式数据库系统,并采用客户服务器方 ...