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. python导包显示No module named XXX问题

    最近用sublime text写python脚本,在导包是一直显示No module named XXX. 问题描述: 首先文件夹的目录结构如下: count.py文件,代码如下: #coding=u ...

  2. Hibernate实例二

    Hibernate实例二 一.测试openSession方法和getCurrentSession方法 hebernate中可以通过上述两种方法获取session对象以对数据库进行操作,下面的代码以及注 ...

  3. Linux 虚拟内存和物理内存的理解

    关于Linux 虚拟内存和物理内存的理解. 首先,让我们看下虚拟内存: 第一层理解 1. 每个进程都有自己独立的4G内存空间,各个进程的内存空间具有类似的结构 2. 一个新进程建立的时候,将会建立起自 ...

  4. poj1651 Multiplication Puzzle

    比较特别的区间dp.小的区间转移大的区间时,也要枚举断点.不过和普通的区间dp比,断点有特殊意义.表示断点是区间最后取走的点.而且一个区间表示两端都不取走时中间取走的最小花费. #include &l ...

  5. Android设计模式之单例模式

    定义 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例 . 单例模式是设计模式中最简单的形式之一.这一模式的目的是使得类的一 ...

  6. CF1083B The Fair Nut and String

    题意 给出两个长度为n的01字符串S和T. 选出k个字典序在S和T之间的长度为n的01字符串,使得尽可能多的字符串满足其是所选字符串中至少一个串的前缀. 这是一道思路比较奇怪的类似计数dp的题. 首先 ...

  7. layedit

    layedit.set({ uploadImage: { url: '' //接口url ,type: '' //默认post } }); //注意:layedit.set 一定要放在 build 前 ...

  8. iterator not dereferencable问题

    STL中的迭代器总是出现各种问题,这个是我在打表达式求值时碰到的... 综合网上的答案,一般来说有两种情况: 第一:访问了非法位置. 一般来说可能在queue为空时取front(),rear(),或者 ...

  9. Java容器涉及的类(代码)

    Customer: public class Customer implements Comparable{ private Integer customerId; private String cu ...

  10. CMD模拟http请求

    搭建环境 前提是在win7中开启telnet服务 开启方法请参考:http://jingyan.baidu.com/article/870c6fc3cd6fa9b03fe4bee4.html 打开Te ...