Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142281    Accepted Submission(s): 33104

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
 
问题的想法是《编程之美》上的,修改了一下提交就ok了。。
思路:
   考虑数组的第一个元素a[0],以及最大的一段数组(a[i].....a[j])跟a[0]之间的关系,有一下几种情况:
     1. 当0=i=j时,元素a[0]本身构成和最大的一段。
     2.当0=i<j时,和最大的一段以a[0]开始。
     3.当0<i时,元素a[0]跟和最大的一段没有关系。
假设已经知道(a[1]....a[n-1])中的最大的一段数组之和为All[1],并且已经知道了(a[1]......a[n-1])中包含a[1]的和最大的一段数组为start[1]。
那么有以上情况可以得出(a[0].....a[n-1])中问题的解All[0]是三种情况的最大值max(a[0],a[0]+start[1],All[1]).
so问题符合无后效性,用动态规划方法可解决。
   
 #include<iostream>
using namespace std;
int main()
{
int t,len,m=,h,n,w,l,r,p,e,i;
cin>>t;h=t;
while(t--)
{
m++;
l=r=p=e=;
cin>>len;
int a[len];
for(i=;i<len;i++)
cin>>a[i];
n=a[],w=a[];l=;p=;
for(i=;i<len;i++)
{
if(a[i]>n+a[i]){
n=a[i];
l=i;
r=i;
}
else {
n=n+a[i];
r=i;
}
if(n>w){
w=n;
e=r;
p=l;
}
}
cout<<"Case "<<m<<":"<<endl<<w<<" "<<p+<<" "<<e+<<endl;
if(m!=h)cout<<endl;
}
return ;
}

hdu_1003_Max Sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

随机推荐

  1. Android 2014年1月22日

    一.广播优先顺序 Android广播有两个很重要的要素:    1 广播 - 用于发送广播 有序广播  -  被广播接收器接收后,可被终止,无法往下继续传达.         典型代表:短信广播 普通 ...

  2. makefile 进阶

    一步一步写一个简单通用的makefile(一) 一步一步写一个简单通用的makefile(二) 一步一步写一个简单通用的makefile(三) 一步一步写一个简单通用的makefile(四)

  3. C语言调用汇编实现字符串对换

    1. 前面配置arm交叉编译环境. 2. 配置好qemu-arm C语言代码string-switch.c: #include <stdio.h> #include <stdlib. ...

  4. Linux方向职业规划

    Linux方向职业分析 引言: 据了解,Linux普通网络管理人员的月薪大约5000元左右,负责编程的Linux软件工程师月薪大约在8000元到12000元之间,Linux嵌入式软件开发人员的月薪大约 ...

  5. C语言宏与单井号(#)和双井号(##)

    C(和C++)中的宏(Macro)属于编译器预处理的范畴,属于编译期概念(而非运行期概念).下面对常遇到的宏的使用问题做了简单总结.关于#和##在C语言的宏中,#的功能是将其后面的宏参数进行字符串化操 ...

  6. hdoj 2036 改革春风吹满地

    改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. cocos 事件转发

    CCDirector在函数setOpenGLView中设置pobOpenGLView,同时会掉用 m_pobOpenGLView->setTouchDelegate(m_pTouchDispat ...

  8. [转]mysql 5.6 存储过程+事务+游标+错误异常抛出+日志写入

    转自:http://www.wolonge.com/post/detail/118249 DELIMITER $$ USE `ecstore`$$ DROP PROCEDURE IF EXISTS ` ...

  9. Conditionals with Omitted Operands (x ? : y)

    Conditionals with Omitted Operands The middle operand in a conditional expression may be omitted. Th ...

  10. myecipse的debug调试操作方法

    在myecipse如果想要查询某个变量的值,或者跟踪程序的执行流程,可以如下操作: 首先在程序中设置好断点(断点的设置方法,就是在想要设置的地方的行首双击,当一个蓝色的圆形实心图标显示出来,就证明你设 ...