前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU-1003 Max Sum(动态规划,最长字段和问题)

HDU-1003 Max Sum(动态规划,最长字段和问题)

作者头像
ShenduCC
发布2018-04-25 17:25:25
1.2K0
发布2018-04-25 17:25:25
举报
文章被收录于专栏:算法修养算法修养

Max Sum

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

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

这是线性动态规划比较简单的最长子段和的问题,状态转移方程 if(dp[i-1]>=0) dp[i]=dp[i-1]+a[i]; else { dp[i]=a[i]; } 这道题目可以用数组,也可以用滚动数组的效果,节省空间、

用一维数组

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;
int n;
int a[100005];
int dp[100005];
int start;
int _end;
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset(dp,0,sizeof(dp));
        _end=1;
        dp[1]=a[1];
        for(int i=2;i<=n;i++)
        {
              if(dp[i-1]>=0)
                  dp[i]=dp[i-1]+a[i];
              else
              {

                  dp[i]=a[i];
              }

        }
        int max=dp[1];
        for(int i=2;i<=n;i++)
        {
            if(max<dp[i])
            {
                max=dp[i];
                _end=i;
            }

        }
        int t1=0;
        start=_end;
        for(int i=_end;i>0;i--)
        {
            t1=t1+a[i];
            if(t1==max)
                start=i;
        }
        cout<<"Case "<<cas<<":"<<endl<<max<<" "<<start<<" "<<_end<<endl;
        if(cas!=t)
            printf("\n");
    }
    return 0;
}

滚动数组

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;
int n;
int a;
int sum;
int _begin;
int _end;

int main()
{
    int t;
    scanf("%d",&t);
    int k=0;
    while(t--)
    {
        int max;
        int x=1;
        scanf("%d%d",&n,&a);
        sum=a;
        max=a;
        _begin=_end=1;
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&a);
            if(sum>=0)
            {
                sum+=a;
            }
            else
            {
                sum=a;
                x=i;
            }
            if(max<sum)
            {
                max=sum;
                _begin=x;
                _end=i;
            }

        }
          cout<<"Case "<<++k<<":"<<endl<<max<<" "<<_begin<<" "<<_end<<endl; 
        if(t)
            cout<<endl;


    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-12-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档