HDU-1003- Max Sum (动态规划)

简介: HDU-1003- Max Sum (动态规划)




Max Sum

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


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:   1176  1087  1069  1058  1203

 



第一种写法:

这是最常见的一种动态写法,看上去很高端


            0   6   -1  1   -6   7   -5

dp[i]     0   6    5   6   0    7   2

p[i]       0   1    1   1   1    1   1


           0    -6    -1    1    -6     7    -5

dp[i]     0    -6    -1    1    -5     7    2

p[i]       0     1     0     0     1     0     1



#include<stdio.h>
#include<iostream>
using namespace std;
#define maxn 100000+10
int num[maxn],dp[maxn],p[maxn];
int main()
{
    int T,n,j;
    scanf("%d",&T);
    for(j=0; j<T; j++)
    {
       int i,sum=0,k=0;
       int start,end;
       cin>>n;
       k++;
       for(i=0;i<n;i++)
          cin>>num[i];
       dp[0]=num[0];
       start=end=1; p[0]=0;
       for(i=1; i<n; i++)
       {
             if(dp[i-1]+num[i]>=num[i]) // 如果前一位置子序列和小雨加上当前位置数据时
             {
                 dp[i]=dp[i-1]+num[i]; //更新当前子序列和
                 p[i]=1;               //  标记当前位置 
          }
          else
          {
              dp[i]=num[i];
              p[i]=0;    //  标记为0 就是子序列的起始位置
          }
       }
       for(i=1; i<n; i++)
       {
             if(dp[i]>dp[0])  // 先找到最大子序列的结束位置
             {
                dp[0]=dp[i];
                end=i+1;   
             }
       }
       start=end;  // 然后从结束位置一次向前找开始位置
       for(i=end; i>=1; i--)
       {
            if(p[i]==1&&p[i-1]==0)   //  如果p[i]=1就继续后退直到 p[i]=0就是这个最大子序列的开始,不懂得可以看上面的模拟数据
            {
                start=i;
                break;
         }
       }
       printf("Case %d:\n%d %d %d\n",j+1,dp[0],start,end);
       if((j+1)!=T) printf("\n");
    }
    return 0;
}





第二种写法:


题目分析:最经典的动态规划,我个人认为动态规划还是比较难理解的,开始接触的时候在网上搜代码几乎都搞不懂。

但是 后来看了一篇博文我才,彻悟!


以a[0]结尾的子序列只有a[0]

以a[1]结尾的子序列有 a[0]a[1]和a[1]

以a[2]结尾的子序列有 a[0]a[1]a[2] / a[1]a[2] / a[2]

……

以a[i]结尾的子序列有a[0]a[1]……a[i-2]a[i-1]a[i]  / a[1]a[2]……a[i-2]a[i-1]a[i] /  a[2]a[3]……a[i-2]a[i-1]a[i] / …… / a[i-1]a[i] / a[i]



所有以a[0] ~a[n]结尾的子序列分组构成了整个序列的所有子序列。


这样,我们只需求以a[0]~a[n]结尾的这些分组的子序列中的每一分组的最大子序列和。然后从n个分组最大子序列和中选出整个序列的最大子序列和。


观察可以发现,0,1,2,……,n结尾的分组中,

maxsum a[0] = a[0]

maxsum a[1] = max( a[0] + a[1] ,a[1])  = max( maxsum a[0] + a[1],a[1])

maxsum a[2] = max( max ( a[0] + a[1] + a[2],a[1] + a[2] ),a[2])  

= max(  max( a[0] + a[1] ,a[1]) + a[2] , a[2])

= max( maxsum a[1] + a[2] , a[2])

..........................

依此类推,可以得出通用的式子。

maxsum a[i] = max( maxsum a[i-1] + a[i],a[i])      maxsum a[i-1] + a[i] 的意思就是 前一个位置的最大值 maxsum a[i-1] 加上当前位置的数据 a[i]

我们从maxsum  a[0]开始算起。

以后的每个就是  maxsum a[i-1] + a[i] 和 a[i] 中取大的那个


如果还不懂:

这里有原文链接可以点击 原文 看一下


#include<stdio.h>
#include<string.h> 
#define INF 0x3f3f3f
int main()
{
  int t,n,a;
  scanf("%d",&t);
  for(int j=1;j<=t;j++)
  {
    scanf("%d",&n);
      int maxsum=-INF,sum=0,temp=1;
      int first,last;
      for(int i=0;i<n;i++)
      {
        scanf("%d",&a);
        sum+=a;
        if(sum>maxsum)
        {
          maxsum=sum;
          first=temp;
          last=i+1;
      }
        if(sum<0)
      {
          sum=0;
        temp=i+2; 
      }   
    }
    printf("Case %d:\n",j);
    if(j==t) printf("%d %d %d\n",maxsum,first,last);
    else printf("%d %d %d\n\n",maxsum,first,last);
  }
  return 0;
}









目录
相关文章
|
12月前
|
存储 算法 安全
LeetCode - #1 Two Sum
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #1 Two Sum
POJ 1844 Sum
POJ 1844 Sum
87 0
|
机器学习/深度学习 Android开发 C++
LeetCode之Two Sum
LeetCode之Two Sum
104 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...
2369 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...
1257 0