码迷,mamicode.com
首页 > 其他好文 > 详细

Max Sum (hdu 1003 简单DP水过)

时间:2015-03-17 12:36:29      阅读:173      评论:0      收藏:0      [点我收藏+]

标签: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;
}



Max Sum (hdu 1003 简单DP水过)

标签:max sum   hdu 1003   简单dp   

原文地址:http://blog.csdn.net/u014422052/article/details/44338193

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!