HDU Problem A [ 最大连续子序列和 ]——基础DP

Problem A

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14   Accepted Submission(s) : 5
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.<br>
 

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).<br>
 

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.<br>
 

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
 
===========================

虽然这道题很明显要dp,但我还是傻傻的用了枚举,想碰一下运气,当然结果o(n^2)的复杂度,超时。。。。。。

正解:o(n)算法

1、本题所用规律:舍去前面的和为负数的子串,从筛选后的子串中选择sum最大的连续子序列。

2、dp思想分析:首先我们想对于一个数来说,如果该数>0,那么加上它后sum是增大的,但如果该数<0,那么它应该被舍去;同理,对于一个数来说是这样,那么对于多个数组成的子序列来说也是这样。或者定义状态方程f[i]表示以第i个数为尾端的子序列中sum的最大值,那么有状态转移方程f[i]=max(f[i-1]+a[i],a[i]),显然当f[i-1]<0时,f[i]=a[i],也就是说如果前i-1个数的和为负数,那么max sum从i位置开始重新算。

3、代码思路:一层for遍历数组,sum记录和,max记录最大值,x记录起始位置,y记录末位置;先求和,如果当前和<0,更改起始位置;如果得到一个sum>maxx,更新末位置与maxx。注意:如果我们得到的sum总是<0,比如说整个数组都是负数,那我们的正确结果应该是其中最大的那个负数,且x==y,但因为前面我们想的是只要sum<0,就更新x,所以最后得到的x并非正确结果,因此,我们对上述思路稍作修改,我们定义一个临时变量 k 来储存 x 的值,只有当更新maxx时,我们才去更新x,这样就能保证最后得到的x是sum最大的子串的起始位置。

AC代码:

#include<iostream>
#include<cstring>
using namespace std;
int a[100100];
int main()
{
    int t,n;
    int flag=1;
    cin>>t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        int sum=0,zx=1,k=1,zy,maxx=-999999999;
        cin>>n;
        for(int i=1;i<=n;++i)cin>>a[i];
        for(int i=1;i<=n;++i)
        {
           sum+=a[i];
            if(sum>maxx)//只有更新maxx时才更新zx,zy,尤其注意zx;
            {
                maxx=sum;
                zy=i;
                zx=k;
            }
            if(sum<0)
            {
                sum=0;
                k=i+1;//用k代替zx临时存储起始位置;
            }
        }
        if(flag>=2)cout<<endl;
        cout<<"Case "<<flag<<":"<<endl;
        cout<<maxx<<" "<<zx<<" "<<zy<<endl;
	flag++;

    }

}

最后,对于自己o(n^2)的代码也贴出来吧(超时):

#include<bits/stdc++.h>
using namespace std;
int a[100100];
int sum[100100];
int main()
{
    int t,n,x,y;
    int flag=1;
    cin>>t;
    while(t--)
    {
        int maxx=0;
        memset(a,0,sizeof(a));
        memset(sum,0,sizeof(sum));
        cin>>n;
        for(int i=1;i<=n;++i)
        {
            cin>>a[i];
            sum[i]=sum[i-1]+a[i];//用了前缀和来优化;
        }
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=i;++j)
            {
                if(maxx<sum[i]-sum[j-1])
                {
                    maxx=sum[i]-sum[j-1];
                    x=i;y=j;
                }

            }
        }
        if(flag>1)cout<<endl;
        cout<<"Case "<<flag<<":"<<endl;
        cout<<maxx<<" "<<y<<" "<<x<<endl;
        flag++;
    }
}


The end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值