Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 330535    Accepted Submission(s): 78678

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

  -   -
- - -

Sample Output

Case :

Case :
  

题目大意与分析

就是给你一个序列,求最大区间和以及该最大区间的下标

动态规划的思想:

  用dp[i]来代表以a[i]为结尾的最大区间和,这样就有两种情况:

    要么dp[i]是正的 那么以a[i]结尾的最大区间和就是dp[i-1]+a[i](因为必须是a[i]结尾,a[i]是正是负都无法取舍的)

    要么dp[i]是负的 那么以a[i]结尾的最大区间和就是a[i]

  状态转移方程:dp[i] = max( dp[i-1]+a[i],  a[i] );

  初始化:因为我们从前往后推,可以将dp数组和a数组合二为一,节约空间

另外需要注意的是 需要三个游标:s、l、r 分别代表当前区间的左端点、最大区间的左端点、最大区间的右断点,至于当前区间的右端点,可以直接用i表示。

代码

#include<bits/stdc++.h>

using namespace std;

int T,n,i,dp[],l,r,s,maxs,t=;

int main()
{
cin>>T;
while(T--)
{
t++;
memset(dp,,sizeof(dp));
cin>>n;
for(i=;i<=n;i++)
{
scanf("%d",&dp[i]);
}
maxs=dp[];
s=;
l=;
r=;
for(i=;i<=n;i++)
{
dp[i]=max(dp[i-]+dp[i],dp[i]);
if(dp[i-]<)
{
s=i;
}
if(dp[i]>maxs)
{
l=s;
r=i;
maxs=dp[i];
}
}
printf("Case %d:\n",t);
printf("%d %d %d\n",maxs,l,r);
if(T)
printf("\n");
}
}

HDU 1003 Max Sum (动态规划 最大区间和)的更多相关文章

  1. hdu 1003 Max Sum(动态规划)

    解题思路: 本题在给定的集合中找到最大的子集合[子集合:集合的元素的总和,是所有子集合中的最大解.] 结果输出: 最大的子集合的所有元素的和,子集合在集合中的范围区间. 依次对元素相加,存到一个 su ...

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

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

  3. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  4. hdu 1003 Max Sum (DP)

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

  5. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

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

  6. hdu 1003 Max Sum (动态规划)

    转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...

  7. HDU 1003 Max Sum 求区间最大值 (尺取法)

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

  8. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  9. HDU 1003 Max Sum

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

随机推荐

  1. 1223 drf引入以及restful规范

    目录 前后台的数据交互 drf 知识点概括 1. 框架安装 2. 接口 2.1 什么是接口 2.2 接口文档 2.3 接口工具的使用 2.4 restful接口规范 debug的使用 前后台的数据交互 ...

  2. __stdcall、__cdcel、__fastcall 调用

    常用的调用约定有stdcall,cdecl,fastcall,thiscall,naked call等,以下将 __stdcall.__cdecl和__fastcall三种函数调用协议加以比较,函数调 ...

  3. 基于Python原生asyncio模块对DNS正向和反向的解析

    一.正向解析:域名解析IP地址 import asyncio import socket domains = [ ('www.baidu.com', 'https'), ('cn.bing.com', ...

  4. k8s-insight测试

    eureka apiVersion: v1 kind: Pod metadata: name: eureka labels: ccb: eureka spec: containers: - name: ...

  5. DOM修改

    ㈠DOM标准 核心DOM:                                                                               HTML DOM ...

  6. head first 设计模式笔记4-工厂模式(简单工厂、工厂方法、抽象工厂)

    1.简单工厂模式 Pizza public abstract class Pizza { abstract void prepare(); abstract void bake(); abstract ...

  7. jquery设置css属性几种方式

    用css()方法返回元素的样式属性 $("div").css("padding-left")); 用css()设置样式 $("div").c ...

  8. 关键字static在标准C/C++的作用

    static总结:根据作用域,存储域,生命周期3点来说 static含义: 是C/C++中很常用的修饰符,它被用来控制变量的存储方式和可见性. (1)标准C语言中,static的最主要功能是隐藏,其次 ...

  9. jQuery_页面加载问题

    运行如下代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  10. zabbix服务端连接客户端报错Received empty response from Zabbix Agent at [192.168.10.105]. Assuming that agent dropped connection because of access permissions

    这是zabbix WEB报的问题:Received empty response from Zabbix Agent at [192.168.10.105]. Assuming that agent ...