Max Sum (hdu 1003 简单DP水过)

简介:

Max Sum

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


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
 

Author
Ignatius.L
 

Recommend
We have carefully selected several similar problems for you:   1058  1203  1257  1421  1024 
 

题意:给出n个数的序列,求出最大的子串和。并输出起点和终点。

思路:dp[i]表示以i为结尾的最大子串和。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int dp[maxn];       //dp[i]表示以i为结尾的最大子串和
int a[maxn];
int n;

int main()
{
    int i,j,t,cas=1;;
    sf(t);
    bool flag=false;
    while (t--)
    {
        if (flag) pf("\n");
        flag=true;
        sf(n);
        FRL(i,1,n+1)
            sf(a[i]);
        mem(dp,0);
        dp[1]=a[1];
        int S=1,T=1,s=1,t=1,maxx=a[1];//s,t记录当前首尾指针。S。T记录当前最大值的首尾指针
        FRL(i,2,n+1)
        {
            if (a[i]>dp[i-1]+a[i])  //假设a[i]对dp[i-1]没有贡献反而会使dp[i-1]减小,那么就以i又一次作为起点
            {
                s=i;
                t=i;
                dp[i]=a[i];
            }
            else if (a[i]<=dp[i-1]+a[i])  //a[i]比dp[i-1]+a[i]更大,就将尾指针t向后移赋为i
            {
                t=i;
                dp[i]=dp[i-1]+a[i];     //更dp[i]
            }
            if (dp[i]>maxx)   //假设当前dp[i]比之前的最大值要大,更新最大值,并记录首尾指针
            {
                S=s;
                T=t;
                maxx=dp[i];
            }
        }
        pf("Case %d:\n",cas++);
        pf("%d %d %d\n",maxx,S,T);
    }
    return 0;
}










本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5077522.html,如需转载请自行联系原作者


相关文章
|
算法
poj 2479 Maximum sum(求最大子段和的延伸)
看完最大连续子段和 的 dp算法 这个很容易理解,我用dplift[i]保存第1到第i个之间的最大子段和,dpright[i]保存第i到第n个之间的最大子段和,最终结果就是dplift[i]+dpright[i+1]中最大的一个。
54 0
|
7月前
|
人工智能 Java
HDU-1003- Max Sum (动态规划)
HDU-1003- Max Sum (动态规划)
43 0
|
C++
【PAT甲级 - C++题解】1007 Maximum Subsequence Sum
【PAT甲级 - C++题解】1007 Maximum Subsequence Sum
90 0
|
机器学习/深度学习
HDU2376——Average distance(思维+树形DP)
HDU2376——Average distance(思维+树形DP)
95 0
|
人工智能
HDOJ/HDU 2710 Max Factor(素数快速筛选~)
HDOJ/HDU 2710 Max Factor(素数快速筛选~)
108 0
|
Go
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
112 0
|
Java 数据安全/隐私保护
[LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数。 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ] 中有 3 个岛。
1252 0
|
存储 人工智能 算法
HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29942    Accepted Submissio...
2405 0