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. ORCAD中的一些操作小技巧

    1.ORCAD中改变元器件和文本字体颜色的命令: 打开在 View -> Toolbar -> Command Window.然后圈选文字(可复选),然后到 Command Window ...

  2. 让uboot的tftp支持上传功能

    转载:http://blog.chinaunix.net/uid-20737871-id-2124122.html uboot下的tftp下载功能是非常重要和常见的功能.但是偶尔有些特殊需求的人需要使 ...

  3. Jenkins--Run shell command in jenkins as root user?

    You need to modify the permission for jenkins user so that you can run the shell commands. You can i ...

  4. webpack 样式分离之The root route must render a single element

    公司项目使用的是webpack1,使用extract-text-webpack-plugin 插件无法将css分离出来,检查原因,发现有如下代码 <Route path="/home& ...

  5. P3382 【模板】三分法

    题目描述 如题,给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减.试求出x的值. 输入输出格式 输入格式: 第一行一次包含一个正整数N和两个实数l.r,含 ...

  6. 广播、多播和IGMP的一点记录

    广播和多播:仅应用于UDP 广播分为: 1.受限的广播(255.255.255.255) 2.指向网络的广播(eg:A类网络 netid.255.255.255)主机号为全1的地址 3.指向子网的广播 ...

  7. 【BZOJ3544】[ONTAK2010]Creative Accounting 前缀和+set

    [BZOJ3544][ONTAK2010]Creative Accounting Description 给定一个长度为N的数组a和M,求一个区间[l,r],使得(\sum_{i=l}^{r}{a_i ...

  8. 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1845 解决:780 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  9. php 整合 微博登录

    现在很多网站都整合了便捷的第三方登录,如QQ登录.新浪微博.搜狐.网易等,为用户提供不少方便和节约时间.我们可以选择使用JS或SDK实现第三方提供用户授权API,本文主要讲解 JAVA SDK 新浪微 ...

  10. shell执行lua脚本传参数

    #lua test.lua 2 5arg[0]= test.lua arg[1]= 2arg[2]= 5 if arg[1] and arg[1] == "2" then prin ...