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 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

分析:

求最大字段和,d[i]表示已 i 结尾(字段和中包含 i )在 a[1..i] 上的最大和,d[i]=(d[i-1]+a[i]>a[i])?d[i-1]+a[i]:a[i];

max = {d[i],1<=i<=n} 这就是状态转移方程

代码:

#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
    int j, i, k, n, m, t;
    int a;
    scanf("%d", &t);
    for (j = 1; j <= t; j++)
    {
        scanf("%d", &n);
        int sum = 0, maxsum = -1001, first = 0, last = 0, temp = 1;
        for (i = 0; i<n; i++)
        {
            scanf("%d", &a);
            sum += a;
            if (sum > maxsum)
            {
                maxsum = sum; first = temp; last = i + 1;
            }
            if (sum < 0)
            {
                sum = 0; temp = i + 2;
            }
        }
        printf("Case %d:\n%d %d %d\n", j, maxsum, first, last);
        if (j != t)
        {
            printf("\n");
        }
    }

    return 0;
}
时间: 2024-11-09 04:36:29

Hdu_1003_Max Sum 解题心得的相关文章

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 s

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

第四章学习小结 串的模式匹配 解题心得体会

串的模式匹配 解题心得体会 关于串,模式匹配是其一个很重要的问题.针对这个问题,书上讲了两种模式匹配的算法,即BF算法和KMP算法,下面针对这两种算法的实现谈谈我的心得. 一.BF算法的探索 [错误代码1] #include<iostream> #include<string.h> using namespace std; typedef struct{ char ch[1000002]; int length; }SString; void Index_BF(SString S,

Binary Tree Maximum Path Sum 解题注意

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree,1/ \2 3Return 6. 解题思路,递归 a b      c curmax = max (a+b, a, a+c) //计算当前节点单边最大值, 如果a+b 最大,那就是说,把左子树包含进来,有利可图 如果a

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

USACO Greedy Gift Givers 解题心得

本题算法不难想出,但是中间还是出现了一些问题. 开始的时候是#11:Execution error,后来把普通的数组改成动态数组后问题消失. 后来又出现了Execution error: Your program had this runtime error: Illegal file open (/dev/tty). 随后google解决方案,多数都是数组开小了.遂开大数组,无效. 突然意识到很有可能是低级错误,于是检查代码. 发现写了个 for(int i = 0; i < n2 ; i++

括号配对问题——解题心得

Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a)if it is the empty string (b)if A and B are correct, AB is correct, (c)if A is correct, (A ) and [A ] is correct. Write a program