Max Sum

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

Total Submission(s): 292444    Accepted Submission(s): 69379

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

题意

给出一个数组,求连续子段和的最大值

思路

第一次写用的前缀和,写完后发现复杂度太高了,交上去果断TLE

翻了一下以前写的51Nod的一道连续字段和的题,写了出来。复杂度O(n)

首先定义一个变量sum,使sum的值足够小,然后开始输入元素x,如果sum<0(即sum+x<x),将sum的值变为x,起点变为当前x的位置(相当于舍弃了前面的sum的值,从x开始重新累加)。然后定义ans,first,end分别记录最终的最大值,起点,终点。sum每改变一次,让ans和sum进行比较,如果ans<sum,更新ans的值,并将起点first为l,终点end变为当前x的位置i,最后输出即可。

一篇关于最大连续子序列和的讲的很不错的博客:https://blog.csdn.net/samjustin1/article/details/52043369。里面还有好多种写法,很详细

AC代码

#include<bits/stdc++.h>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=1e6+10;
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
int _=0;
while(t--)
{
int n;
cin>>n;
int m;
ll ans=INT_MIN;
ll sum=INT_MIN;
int l;
// sum,sum为临时记录最大值和起点位置的变量
int first,end;
for(int i=1;i<=n;i++)
{
cin>>m;
//如果sum小于0,让sum从m重新开始
//并改变l的值
if(sum+m<m)
{
sum=m;
l=i;
}
else
sum+=m;
// 如果ans小于sum,更新ans,first,end
if(ans<sum)
{
first=l;
end=i;
ans=sum;
}
}
cout<<"Case "<<++_<<':'<<endl;
cout<<ans<<" "<<first<<" "<<end<<endl;
// 注意输出。最后一组样例中没有空行,前面每组之间都有空行
if(t)
cout<<endl;
}
return 0;
}

HDU 1003:Max Sum(DP,连续子段和)的更多相关文章

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

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

  2. hdu 1003 Max Sum (DP)

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

  3. [ACM] hdu 1003 Max Sum(最大子段和模型)

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

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

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

  5. 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 ...

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

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

  7. hdu 1003 Max sum(简单DP)

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

  8. HDU 1003 Max Sum(DP)

    点我看题目 题意 : 就是让你从一个数列中找连续的数字要求他们的和最大. 思路 : 往前加然后再判断一下就行. #include <iostream> #include<stdio. ...

  9. hdu 1003 Max Sum(基础dp)

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

  10. hdu 1024 Max Sum Plus Plus (子段和最大问题)

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

随机推荐

  1. RabbitMQ入门_15_访问控制

    参考资料:https://www.rabbitmq.com/access-control.html A. 核心概念 Virtual Host:虚拟主机为 RabbitMQ 中的资源提供了逻辑分组与隔离 ...

  2. MINA2.0用户手册中文版

    MINA2.0用户手册中文版--第一章 MINA2.0入门 MINA2.0用户手册中文版--第二章 第一节 MINA应用程序架构 MINA2.0用户手册中文版--第二章 第二节 TCP服务端实例 MI ...

  3. typescripts学习

    可选与默认参数 可选参数:在参数名后面,冒号前面添加一个问号,则表明该参数是可选的.如下代码: function buildName(firstName: string, lastName?: str ...

  4. jsp/servlet区别

    简介: JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Microsystems公司倡导.许多公司参与一起建立的一种动态 ...

  5. Windows Server2008安装mysql5.6出现程序无法正常启动(0xc000007b)

    下载 到官网下载mysql5.6版本,msi安装包只有32位无64位 移动到指定文件夹下,解压文件 添加环境变量 变量名:MYSQL_HOME 变量值:C:\Program Files\mysql 即 ...

  6. 一篇文章,读懂Netty的高性能架构之道

    一篇文章,读懂Netty的高性能架构之道 Netty是由JBOSS提供的一个java开源框架,是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架, ...

  7. hdu 1226 bfs+余数判重+大数取余

    题目: 超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. POJ-2415 Hike on a Graph (BFS)

    Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...

  9. JS之Callback function(回调函数)

    JS中的回调函数: 1.概念: 函数a有一个参数,这个参数是个函数b,当函数a执行完以后执行函数b,那么这个过程就叫回调,即把函数作为参数传入到另一个函数中,这个函数就是所谓的回调函数. 2.举例: ...

  10. Oracle 等待事件 db file sequential read

    db file sequential read-数据文件顺序读取 等待事件: "db file sequential read" Reference Note (文档 ID 345 ...