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
 
???->>>>设某序列[x,j]的和大于等于0,则后面再来一个数肯定要接在上面;如果其小于0则后面来的数不要接在其后面,要把这个序列的开头设为当前输入的位置。
 
 #include <iostream>
using namespace std; int main(){
int t,n,temp,pos1,pos2,max,now,x,i,j;
cin>>t;
for (i=;i<=t;i++){
cin>>n>>temp;//输入n和第一个数
now=max=temp;//令now和max都等于第一个数
pos1=pos2=x=;//令初始位置为1
for(j=;j<=n;j++){//循环输入2-n个
cin>>temp;
if (now+temp<temp)//如果now为负就从新的位置开始
now=temp,x=j;//x保存当前开头位置
else//否则就在now后加上输入数据
now+=temp;
if(now>max)//如果now>max就更新max
max=now,pos1=x,pos2=j;
}
cout<<"Case "<<i<<":"<<endl<<max<<" "<<pos1<<" "<<pos2<<endl;
if (i!=t)cout<<endl;
}
return ;
}
 

[ACM_动态规划] hdu1003 Max Sum [最大连续子串和]的更多相关文章

  1. 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾

    2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...

  2. HDU-1003 Max Sum(动态规划,最长字段和问题)

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

  3. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

  4. HDU--1003 Max Sum(最大连续子序列和)

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

  5. HDU1003 Max Sum(求最大字段和)

    事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...

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

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

  7. hdu1003 Max Sum(经典dp )

      A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descr ...

  8. (动态规划)Max Sum Plus Plus--hdu--1024

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Othe ...

  9. ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)

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

随机推荐

  1. iOS 网络请求中的空类型字符串转换

    创建一个工具类,   .h: #import <Foundation/Foundation.h> @interface MySetNullWithStrTool : NSObject +( ...

  2. Java中的NIO及IO

    1.概述 Java NIO(New IO) 是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同, ...

  3. maven的下载

    1.maven的下载地址:http://maven.apache.org/download.cgi.下载3.3.9版本(在选择下载的版本是要与JDK契合). 2.下载之后,解压的文件放的位置尽量不要有 ...

  4. Mina入门:mina版之HelloWorld[z]

    Mina入门:mina版之HelloWorld [z] 一,前言: 在完成上篇文章<Mina入门:Java NIO框架Mina.Netty.Grizzly简介与对比>之后,我们现在可以正式 ...

  5. 删除SVN版本控制目录

    @echo On @Rem 删除SVN版本控制目录 @PROMPT [Com] @for /r . %%a in (.) do @if exist "%%a\.svn" rd /s ...

  6. linux中的hosts文件

    1.主机名: 无论在局域网还是INTERNET上,每台主机都有一个IP地址,是为了区分此台主机和彼台主机,也就是说IP地址就是主机的门牌号. 公网:IP地址不方便记忆,所以又有了域名.域名只是在公网( ...

  7. NGS的duplicate的问题

    NGS的duplicate的问题   duplicate的三个问题: 一.什么是duplicate? 二.duplicate来源? 三.既然PCR将1个reads复制得到成百上千copies,那为什么 ...

  8. 什么是adb命令?以及如果高效使用他们?

    接下来 我会为大家讲解 最通俗易懂的adb命令 希望大家能够喜欢...

  9. java Exception in thread "main" java.lang.NoClassDefFoundError: main (wrong name: pm/main)

    javac main.java 编译后没有问题 java main 出现下面在问题: Exception in thread "main" java.lang.NoClassDef ...

  10. android的 Base64

    byte[] key=Base64.decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4".getBytes(), Base64.DEFAULT);   ...