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. Python 3标准库 第十四章 应用构建模块

    Python 3标准库 The Python3 Standard Library by  Example -----------------------------------------第十四章   ...

  2. 【Winform-ComboBox】实现ComboBox下拉框与数据库的绑定

    实现效果如下: 1.设计窗体 下拉框的名称cmbName 2.连接数据库 DBHelper类代码: class DBHelper { /// <summary> /// 创建静态连接字符串 ...

  3. mybatis解析和基本运行原理

    Mybatis的运行过程分为两大步: 第1步,读取配置文件缓存到Configuration对象,用于创建SqlSessionFactory: 第2步,SqlSession的执行过程.相对而言,SqlS ...

  4. linux运维、架构之路-Kubernetes基础(一)

    一.Kubernetes介绍 Kubernetes最初源于谷歌内部的Borg,提供了面向应用的容器集群部署和管理系统.Kubernetes的目标旨在消除编排物理/虚拟计算,网络和存储基础设施的负担,并 ...

  5. PHP关于对象访问静态方法、属性等问题

    为何有这样的问题呢?源自一段代码,如下: class A { // public static $name = 'wangyumeidsb'; public $name = 'woaini'; pub ...

  6. Python天天学_01_基础1

    Python_day_01 金角大王:http://www.cnblogs.com/alex3714/articles/5465198.html ------Python是一个优雅的大姐姐 学习方式: ...

  7. vue路由 routers的写法:require用与不用

    vue路由的写法有很多种,这里我只说routers的写法,一种是compcomponent后面直接写路径,另一种是用require的方式,来看代码 import Vue from 'vue' impo ...

  8. Anaconda cheat sheet

    1 anaconda prompt 闪退的情况 在cmd中进入C:\ProgramData\Anaconda3\Scripts然后可以使用各种conda命令 2 anaconda 换源 # 方法参考 ...

  9. 【Java基础】谈谈集合.List

    摘自:https://www.cnblogs.com/54chensongxia/p/11722828.html 目录 1. ArrayList 1.1 ArrayList的构造 1.2 add方法 ...

  10. 网页中JS函数自动执行常用三种方法

    (1)最简单的调用方式,直接写到html的body标签里面:        <body onload="myFunction()"></body>      ...