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. VTK学习之路——画画我的小苹果

    数据集主要由描写叙述数据集几何形状的点集数据及构成数据集的单元构成,因此构建数据集的主要任务就是确定点集和构建单元,本演示样例程序构建了一个苹果的实体,然后绘制苹果.演示样例程序运行的过程例如以下: ...

  2. 使用css counter来美化代码片段的样式

    博客园默认的代码片段样式不太美观,特别是复制代码时会把前面的行号也复制下来,操作起来比较麻烦.最近看到一种使用CSS计数器来美化代码片段的方法,于是研究了一下计数器的使用,在此做个笔记. 这是官网的例 ...

  3. python学习(十)赋值、表达式、if、while、for

    明天以搞定这几个应该不难 赋值.表达式.if.while.for 函数.作用域.参数.函数高级话题 迭代和解析一.二 还有我的<30天自制操作系统>没看 #!/usr/bin/python ...

  4. Linux下服务端口被占用

    有一次,在启动ejabberd的时候,报错如下: 10:30:15 =CRASH REPORT==== crasher: initial call: supervisor:ejabberd_liste ...

  5. 多媒体开发之---h264 高度和宽度获取

    ( School of Computer Science & Technology, Soochow University,SuZhou 215006:) Abstract: H.264 is ...

  6. 怎样实现动态加入布局文件(避免 The specified child already has a parent的问题)

    首先扯点别的:我应经连续上了两个星期的班了,今天星期一.是第三个周.这个班上的也是没谁了.近期老是腰疼. 预计是累了.近期也没跑步.今天下班继续跑起. 这篇文章讲一讲怎样在一个布局文件里动态加在一个布 ...

  7. C#泛型<T>说明

    泛型:即通过参数化类型来实现在同一份代码上操作多种数据类型.泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用. C#泛型的作用概述 C#泛型赋予了代码更强的类型安全,更 ...

  8. python classmethod方法 和 staticmethod

    classmethod() 是一个类方法,用来装饰对应的函数.被classmethod 装饰之后就无需实例化,也不需要在函数中传self,但是被装饰的函数第一个参数需要是cls来表示自身类.可以用来调 ...

  9. 【BZOJ3217】ALOEXT 替罪羊树+Trie树

    [BZOJ3217]ALOEXT Description taorunz平时最喜欢的东西就是可移动存储器了……只要看到别人的可移动存储器,他总是用尽一切办法把它里面的东西弄到手. 突然有一天,taor ...

  10. SQL 经验总结

    总结日常工作中使用SQL遇到的坑和技巧,一是加深印象,二是方便回顾.会不定期更新. 坑 1.多表联查时要使用表名,如果两个表的有列名相同的情况你没有加别名,那么sql编译器就不知道使用哪个列.这时进行 ...