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

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]; } 这道题目可以用数组,也可以用滚动数组的效果,节省空间、

用一维数组

#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;
}

滚动数组

#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;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏算法修养

ZOJ 3661 Palindromic Substring(回文树)

Palindromic Substring ---- Time Limit: 10 Seconds      Memory Limit: 65536 KB --...

29760
来自专栏ml

POJ--Strange Way to Express Integers

Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K ...

28370
来自专栏算法修养

HDU 3578 Greedy Tino(双塔DP)

Greedy Tino Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768...

35150
来自专栏ml

HDUOJ---大菲波数

大菲波数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java...

300110
来自专栏算法修养

CodeForces Roads not only in Berland(并查集)

H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB    ...

26150
来自专栏算法修养

HDU 3388 Coprime(容斥原理+二分)

Coprime Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (...

33850
来自专栏ml

HDUOJ------1711Number Sequence

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3...

30960
来自专栏算法修养

POJ-1458 Common Subsequence(线性动规,最长公共子序列问题)

Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submis...

33370
来自专栏ml

hdu 4315 Climbing the Hill(阶梯博弈转nim博弈)

Climbing the Hill Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/...

365110
来自专栏算法修养

HDU 5676 ztr loves lucky numbers

ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others)    Me...

37650

扫码关注云+社区

领取腾讯云代金券