hdu_1003_Max Sum

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问题符合无后效性,用动态规划方法可解决。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int t,len,m=0,h,n,w,l,r,p,e,i;
 6     cin>>t;h=t;
 7     while(t--)
 8     {
 9         m++;
10         l=r=p=e=0;
11         cin>>len;
12         int a[len];
13         for(i=0;i<len;i++)
14             cin>>a[i];
15          n=a[0],w=a[0];l=0;p=0;
16          for(i=1;i<len;i++)
17          {
18             if(a[i]>n+a[i]){
19                 n=a[i];
20                 l=i;
21                 r=i;
22             }
23             else {
24                 n=n+a[i];
25                 r=i;
26             }
27             if(n>w){
28                 w=n;
29                 e=r;
30                 p=l;
31             }
32         }
33         cout<<"Case "<<m<<":"<<endl<<w<<" "<<p+1<<" "<<e+1<<endl;
34         if(m!=h)cout<<endl;
35     }
36     return 0;
37 }

hdu_1003_Max Sum

时间: 2024-08-06 15:25:24

hdu_1003_Max Sum的相关文章

hdu_1003_Max Sum hdu_1058_Humble Numbers hdu_1059_Dividing

hdu_1003_Max Sum http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意是给出一组数,求出这组数的一个最大的连续子序列的和,并且要求出序列的左右区间. 这道题不会做,膜拜后的思想是,一个连续序列s要加上一个数a,除非该连续序列s的sum已经>0,否则a可以独立成一个序列s'.于是,用maxsum记录最大的序列和,l和r记录左右区间. #include <cstring> #include <iostream> u

Hdu_1003_Max Sum 解题心得

原题: 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

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

Leetcode 494 Target Sum 动态规划 背包+滚动数据

这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20))和一个数S c1a1c2a2c3a3......cnan = S, 其中ci(1<=i<=n)可以在加号和减号之中任选. 求有多少种{c1,c2,c3,...,cn}的排列能使上述等式成立. 例如: 输入:nums is [1, 1, 1, 1, 1], S is 3. 输出 : 5符合要求5种

31.SUM() 函数

SUM() 函数 SUM 函数返回数值列的总数(总额). SQL SUM() 语法 SELECT SUM(column_name) FROM table_name SQL SUM() 实例 我们拥有下面这个 "Orders" 表: O_Id OrderDate OrderPrice Customer 1 2008/12/29 1000 Bush 2 2008/11/23 1600 Carter 3 2008/10/05 700 Bush 4 2008/09/28 300 Bush 5

1305 Pairwise Sum and Divide

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = i+1 to A.length sum = sum + Floor((A[i]+A[j])/(A[i]*A[j])) return sum 给出数组A,由你来计算fun(A)的结果.例如:A = {1, 4, 1},fun(A) = [5/4] + [

Java [Leetcode 303]Range Sum Query - Immutable

题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You may assume that the ar

LeetCode 303. Range Sum Query - Immutable

求数组nums[i,j]的和 思路:另开一sum数组,sum[i]为nums[0,i]的和,所以nums[i,j] = sum[j] - sum[i-1] 1 class NumArray { 2 public: 3 vector<int> sum; 4 NumArray(vector<int> &nums) { 5 sum.resize(nums.size(), 0); 6 sum[0] = nums[0]; 7 int len = nums.size(); 8 for(