Max Sum

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
 

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n,m,a,temp,start,end;
    long sum,max;
    cin>>n;
    for(int j=1;j<=n;j++)
    {
       cin>>m;
       sum=0;
       max=-1001;////由于存在全是负数,将最大值赋值为负数(更大也行)
       temp=1;
       for(int i=1;i<=m;i++)
       {
               cin>>a;
               sum+=a;
               if(sum>max)
               {
                  max=sum;
                  start=temp;//更新起始点
                  end=i;  //更新末点
               }
               if(sum<0)
               {
                  sum=0;
                  temp=i+1;//执行下一个i(也就是读取下一个数字)
               }
       }
       cout<<"Case "<<j<<":"<<endl;
       cout<<max<<" "<<start<<" "<<end<<endl;
       if(j<n)
       cout<<endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

杭电1003-Max Sum的更多相关文章

  1. 杭电1003 Max Sum 【连续子序列求最大和】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...

  2. 杭电1003 Max Sum TLE

    这一题目是要求连续子序列的最大和,所以在看到题目的一瞬间就想到的是把所有情况列举出来,再两个两个的比较,取最大的(即为更新最大值的意思),这样的思路很简单,但是会超时,时间复杂度为O(n^3),因为有 ...

  3. 杭电 1003 Max Sum (动态规划)

    参考:https://www.cnblogs.com/yexiaozi/p/5749338.html #include <iostream> #include <cstdio> ...

  4. 杭电1003 MAX SUN

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

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

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

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

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

  7. hdu 1003 Max Sum (DP)

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

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

  10. #1003 Max Sum

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 给你一串数列,让你求出其中 一段连续的子数列 并且 该子数列所有数字之和最大,输出该子数列的和.起点与终点 ...

随机推荐

  1. Java Socket常见异常处理 和 网络编程需要注意的问题

    在java网络编程Socket通信中,通常会遇到以下异常情况: 第1个异常是 java.net.BindException:Address already in use: JVM_Bind. 该异常发 ...

  2. PostgreSQL 系统的基本体系结构

    PostgreSQL 使用客户机/服务器(C/S)的模式提供服务,一个PostgreSQL会话由下列相关的进程(程序)组成: (1)一个服务器端进程.该进程管理数据库文件,接受客户端与数据库的连接,且 ...

  3. 监控linux服务器网卡流量

    监控linux服务器网卡流量 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.       欢迎加入:高级运维工程师之路 598432640 前言:众所周知,我们安装zabbix服务器 ...

  4. navicat的简单应用

    首先  创建连接 主机名 : 可以不写名称随意 主机名/IP地址:localhost或者127.0.0.1 都是本机的意思 端口:默认3306   尽量不要改怕与其余端口重复,如有重名端口系统会报错 ...

  5. JavaScript读写脚txt文件

    1.cmd切换到“C:\Windows\System32>”下,执行“regsvr32 Scrrun.dll” 2.JavaScript读写txt文本代码如下,注意要发布到服务器上 <!D ...

  6. have you declared this activity in your AndroidManifest.xml

    对于那些刚开始接触安卓的开发者来说,遇到这个问题再正常不过了,出现这种问题的原因大概可分为: 1.android的四大组件都必须在AndroidMainifest.xml里面声明,所以首先看看有没有在 ...

  7. php初探

    1.php中的连接符.可以连接多个字符串,相当于java中的+ 2.echo必须与后面的输出内容有至少一个空格 3.php编程中每个结尾都需要添加分号

  8. java的I/O操作:文件的路径

    package solutions; import java.io.*; /** * Created by Administrator on 2016/3/14. */ public class Re ...

  9. <c:if>标签的使用

    <c:if>标签用来在页面中实现条件化的判断功能.它的主要目的就是替换Java脚本中的if语句,来实现页面内容的条件化输出功能.这个标签所进行的判读主要是依据表达式来进行的,如果该表达式的 ...

  10. ZOJ 3645 BiliBili(高斯消元)

    Shirai Kuroko is a Senior One student. Almost everyone in Academy City have super powers, and Kuroko ...