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. ThinkPHP分页链接支持数组参数的办法

    这几天在用ThinkPHP做系统,搜索页有个数组参数提交 <input class="params_t" name="t[]" type="ch ...

  2. mac osx 启动wireshark闪退

    wireshark启动会提示安装x11 去x11地址安装后 启动还是闪退 原来是姿势不对 这样才行~~ 这一步 这个路径一定要对!路径一定要对!路径一定要对! 然后报错不用管它,如果没反应了,就继续等 ...

  3. navicat 的查询功能

    navicat的查询的位置在: 在编辑器界面写代码,代码完成后点左上角的运行. 代码: create(创建)  table(一个表) <xxx>尖括号内的内容必填——我要创建并查询一个名叫 ...

  4. Google 推出的 Java 编码规范(转)

    原文地址:http://www.dahuatu.com/1225/988516.html 原文地址:http://www.dahuatu.com/1225/988516.html 原文地址:http: ...

  5. linux第4天 shell socket

    $[ ] 表示形式告诉shell对方括号中的表达式求值 echo $[3+9] 赋值运算符 =,+=,-=,*=,/=,%=,&=,^=.|=,<<=,>>= let ...

  6. spark standalone zookeeper HA部署方式

    虽然spark master挂掉的几率很低,不过还是被我遇到了一次.以前在spark standalone的文章中也介绍过standalone的ha,现在详细说下部署流程,其实也比较简单. 一.机器 ...

  7. EBS R12中重新enable失效用户之后,丢失职责

    以下请求跑完不能立即生效,需要等上一段时间! oracle support说这并不是一个bug,是一个问题,呵呵,bug和问题,都是你的错,oracle! 工 作中将某个用户失效之后,有可能又需要重新 ...

  8. Logic BIST

    Logic BIST is crucial for many applications, in particular for life-critical and mission-critical ap ...

  9. scan cell

    scan cell有两种不同的input: 1)data input:由电路的combinational logic驱动: 2)scan input:由另一个scan cell驱动,从而形成scan  ...

  10. switch结构2016/03/08

    Switch  03/08 一.结构 switch(){  case *: ;break;……default: ;brek;} 练习:输入一个日期,判断这一年第几天? Console.Write(&q ...