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,如需转载请自行联系原作者


相关文章
|
6月前
|
人工智能 Java
HDU-1003- Max Sum (动态规划)
HDU-1003- Max Sum (动态规划)
37 0
POJ 1844 Sum
POJ 1844 Sum
104 0
|
机器学习/深度学习 Android开发 C++
LeetCode之Two Sum
LeetCode之Two Sum
118 0
|
算法 索引 C++
|
存储 人工智能 算法
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...
2398 0
|
人工智能 Java
HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 593...
1274 0