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. zoj 1221 Risk Flory

    博客开了快半年了- -学习编程也快1年半了,觉得空空的不太好看,刚好最近开始练习ACM了,就来做一个简单的ACM学习笔记吧,纪念的第一题zol 1221 Risk 风险游戏(个人觉得是这样翻- -翻译 ...

  2. wxWidgets一个界面与数据分离的简单例子

    /*************************************************************** * Name: MyApp.h * Purpose: Defines ...

  3. wand(weak and)算法基本思路

    一般搜索的query比较短,但如果query比较长,如是一段文本,需要搜索相似的文本,这时候一般就需要wand算法,该算法在广告系统中有比较成熟的应该,主要是adsense场景,需要搜索一个页面内容的 ...

  4. Linux下常用I/O模型

    Linux异步I/O是Linux内核中提供的一个相当新的增强.它是2.6版本内核的一个标准特性,异步非阻塞I/O背后的基本思想是允许进程发起很多I/O操作,而不用阻塞或等待任何操作完成.稍后或在接收到 ...

  5. 第十一章、认识与学习 BASH Bash Shell 的操作环境

    bash中的变量动不动就说环境变量,真是奇怪,bash只是一个c语言编写的程序而已,跟环境变量有什么关系?如果知道dos的历史的话就知道有个时代是只有命令行界面而没有图形用户界面,这只小小的程序就包揽 ...

  6. CodeIgniter开发实际案例-新闻网站【转】

    CodeIgniter开发实际案例-新闻网站 转:http://blog.csdn.net/ict2014/article/details/22104711?utm_source=tuicool&am ...

  7. 20169210《Linux内核原理与分析》第六周作业

    这次博客分为两部分,第一部分是实验,第二部分是教材. 第一部分:实验 这周实验的内容是使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用,教学案例给的是time()获取系统时间的函数,我 ...

  8. 从零开始学C++之数据封装与抽象:分别用C和C++来实现一个链栈

    下面通过分别用C和C++来实现一个链栈(链表实现),从中体会数据封装抽象的思想: C语言实现:  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  9. ios7新特性实践

    一 iOS 7 新特性:视图控制器切换API 二 iOS 7系列译文:认识 TextKit 三 iOS 7系列译文:iOS7的多任务处理 四 iOS7 最佳实践:一个天气应用案例(上) 五 iOS7 ...

  10. oracle15 pl/sql 分页

    PL/SQL分页 编写分页过程 无返回值的存储过程 古人云:欲速则不达,为了让大家伙比较容易接受分页过程编写,我还是从简单到复杂,循序渐进的给大家讲解.首先是掌握最简单的存储过程,无返回值的存储过程: ...