Max Sum

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

Total Submission(s): 161294    Accepted Submission(s): 37775

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
 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for you:  

pid=1058" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1058 1203 1257 1421 1024 

 

题意:给出n个数的序列,求出最大的子串和。并输出起点和终点。

思路:dp[i]表示以i为结尾的最大子串和。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; int dp[maxn]; //dp[i]表示以i为结尾的最大子串和
int a[maxn];
int n; int main()
{
int i,j,t,cas=1;;
sf(t);
bool flag=false;
while (t--)
{
if (flag) pf("\n");
flag=true;
sf(n);
FRL(i,1,n+1)
sf(a[i]);
mem(dp,0);
dp[1]=a[1];
int S=1,T=1,s=1,t=1,maxx=a[1];//s,t记录当前首尾指针。S。T记录当前最大值的首尾指针
FRL(i,2,n+1)
{
if (a[i]>dp[i-1]+a[i]) //假设a[i]对dp[i-1]没有贡献反而会使dp[i-1]减小,那么就以i又一次作为起点
{
s=i;
t=i;
dp[i]=a[i];
}
else if (a[i]<=dp[i-1]+a[i]) //a[i]比dp[i-1]+a[i]更大,就将尾指针t向后移赋为i
{
t=i;
dp[i]=dp[i-1]+a[i]; //更dp[i]
}
if (dp[i]>maxx) //假设当前dp[i]比之前的最大值要大,更新最大值,并记录首尾指针
{
S=s;
T=t;
maxx=dp[i];
}
}
pf("Case %d:\n",cas++);
pf("%d %d %d\n",maxx,S,T);
}
return 0;
}

Max Sum (hdu 1003 简单DP水过)的更多相关文章

  1. Max Sum -- hdu -- 1003

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

  2. Max Sum—hdu1003(简单DP) 标签: dp 2016-05-05 20:51 92人阅读 评论(0)

    Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  3. HDU 1024 Max Sum Plus Plus 简单DP

    这题的意思就是取m个连续的区间,使它们的和最大,下面就是建立状态转移方程 dp[i][j]表示已经有 i 个区间,最后一个区间的末尾是a[j] 那么dp[i][j]=max(dp[i][j-1]+a[ ...

  4. HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. HDU 1024 Max Sum Plus Plus(DP的简单优化)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  6. HDU 1024:Max Sum Plus Plus(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...

  7. HDU 1024 Max Sum Plus Plus【DP】

    Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...

  8. HDU 1024:Max Sum Plus Plus(DP,最大m子段和)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. HDU OJ Max sum 题目1003

     #include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...

随机推荐

  1. windows和linux套接字中的select机制浅析

    先来谈谈为什么会出现select函数,也就是select是解决什么问题的? 平常使用的recv函数时阻塞的,也就是如果没有数据可读,recv就会一直阻塞在那里,这是如果有另外一个连接过来,就得一直等待 ...

  2. DM6446开发攻略——u-boot-1.3.4移植(1)

    http://zjbintsystem.blog.51cto.com/964211/282387转载   UBOOT的版本更新速度比较快,截止今天,稳定正式的版本是u-boot-2009.11-rc2 ...

  3. python 站点爬虫 下载在线盗墓笔记小说到本地的脚本

    近期闲着没事想看小说,找到一个全是南派三叔的小说的站点,决定都下载下来看看,于是动手,在非常多QQ群里高手的帮助下(本人正則表達式非常烂.程序复杂的正则都是一些高手指导的),花了三四天写了一个脚本 须 ...

  4. readline-6.3 之arm平台交叉编译

    近期须要弄个CLI命令接口程序,初步设想是须要支持历史命令翻阅,tab键命令补全这种一个东西.经查阅相关文档,深耕百度一番!(google近期不太正常) 实在恼火.发现readline果真是个好东西, ...

  5. 调用函数的ALV、面向对象的ALV设置带选择列

    这个就是通过对应的选择列,实现对ALV数据的选择,在调用函数的ALV和面向对象的ALV实现方法存在差异,下面讲两者的方法:1)调用函数的ALV.   通过 SLIS_LAYOUT_ALV-BOX_FI ...

  6. delphi实现穿XP防火墙

    procedure TForm1.Button1Click(Sender: TObject);var   FwMgr,Profile,FwApp: variant;begin   FwMgr := C ...

  7. Indy10.2.5的危险做法

    为了排查一个Bug今天无意看了看Indy源码,结果吓了一跳.TIdIOHandler.ReadLongWord函数用于读取通讯数据并转换成LongWord类型返回,它做用了一种危险的做法可能会导致数据 ...

  8. 双向DFS模板题

    B. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. byte为什么要与上0xff(转)

    无意间翻看之间的代码,发现了一段难以理解的代码. byte[] bs = digest.digest(origin.getBytes(Charset.forName(charsetName))) ; ...

  10. Swift - 二进制,八进制,十六机制的表示方法

    当前位置: 首页 > 编程社区 > Swift > Swift - 二进制,八进制,十六机制的表示方法 Swift - 二进制,八进制,十六机制的表示方法 2015-01-23 14 ...