HDU1003 Max Sum 解题报告

目录

  • 题目信息

    • Problem Description
    • Input
    • Output
    • Sample Input
    • Sample Output
  • 题解
    • 思路
  • 源代码

题目信息

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.

给出一个序列a[1],a[2],a[3]......a[n],你的任务是计算子序列的最大和,例如,对于(6,-1,5,4,-7),最大和是这个子序列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).

第一行包括一个整数T(1<=T<=20),代表测试用例的数量。接下来的T行,每行开始于一个数N(1<=N<=100000),然后跟着N个数,所有的整数都在 -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.

对于每个测试用例,你应该输出两行。第一行是Case #:,#代表测试用例的序号。第二行包括三个数,序列最大和,还有这个子序列的开始和结束坐标。如果有超过一个以上的结果,输出第一个即可。在两个用例之间输出一个空行。

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

题解

思路

典型的动态规划问题,可参见【最长子序列和】。

本题要求输出满足条件的子序列区间,一个for循环就可以搞定

源代码

//HDU 1003 VELSCODE VER1.0
#include <iostream>

using namespace std;

int T,N,max1[20],L[20],R[20];

int A[100000],DP[100000];

int main()
{
    //input
    scanf("%d",&T);
    for( int t=0;t<T;t++ )
    {
        scanf("%d",&N);
        for( int i = 0; i < N; i++ )
        {
            scanf("%d",&A[i]);
        }

        //dp
        DP[0] = A[0];

        for(int i=1;i<N;i++)
        {
            if( A[i] > DP[i-1]+A[i] )
            {
                DP[i] =  A[i];
            }
            else //A[i] < DP[i-1]+A[i]
            {
                DP[i] = DP[i-1]+A[i];
            }
        }

        //寻找最大值
        int pos = 0;

        max1[t] = DP[0];

        for(int i=0;i<N;i++ )
        {
            if( max1[t] < DP[i] )
            {
                max1[t] = DP[i];
                pos = i;
            }
        }

        //寻找子序列左右坐标
        R[t] = pos;
        L[t] = pos; 

        for( int i = pos - 1 ; i >= 0 ;i--  )
        {
            if( DP[i] >= 0 )
            {
                L[t] = i;
            }
            else
                break;
        }
    }

    //output
    for( int i = 0; i < T; i++ )
    {
        printf("Case %d:\n",i+1);
        printf("%d %d %d\n",max1[i],L[i]+1,R[i]+1);
        if( i != T-1 )
        printf("\n");
    }

}

原文地址:https://www.cnblogs.com/velscode/p/10072309.html

时间: 2024-08-28 09:31:22

HDU1003 Max Sum 解题报告的相关文章

Winter-1-D Max Sum 解题报告及测试数据

Time Limit:1000MS Memory Limit:32768KB 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 f

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

2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare:连续区间最大和 @data:2017/9/6 */ #include <bits/stdc++.h> using namespace std; int main() { //freopen("in.txt","r",stdin); int cas; cin

HDU1003 Max Sum

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意:给你一组数字,求出最大的字段和. 思路:这是一个经典的dp题目,定义数组a储存一组数字,a[j]为ji个数,dp[j]表示已j结尾的最大字段和,那么dp[j]=max(dp[j-1]+a[j],dp[j]). 例如: a[]       6   -1   5    4    -7 dp[]     6    5   10  14    7 代码如下: #include <iostream

HDU1003 Max Sum 最大子序列和的问题【四种算法分析+实现】

就拿杭电OJ上的第1003题开始吧,这题比原书要复杂一些. 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 fi

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

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

hdu1003 Max Sum(经典dp )

A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 thi

USACO Section2.3 Zero Sum 解题报告 【icedream61】

zerosum解题报告------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给你N. 把1到N依次写出,每相邻两个数字中间加上一个字符,三选一:'+','-',' '. 如此,便可形成很多表达式,把其中计算结果为0的按字典序输出.[数据范围] 3<=N<

HDU1003 Max Sum(最大连续子序列和)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1003 简单dp,状态转移方程:sum[i] = max{sum[i-1]+a[i],a[i]}. (sum[i]记录以a[i]为子序列末端的最大连续和.) 对于a[i]这个数字,我们考虑是否将它选入之前连续的序列. 如果选,状态变为sum[i-1]+a[i] ; 如果不选,则从此开始一个新的序列,故和为a[i]. 1 #include<cstdio> 2 int main() 3 { 4 int T,