1004 Max Sum

Max Sum

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

      这道题是和上一道1003几乎一样的题,我没再多想,稍微改了改输出目标,就交了,运行结果无误,但是没A,又改又试了几遍,依然没A,没有弄明白原因,只好换方法。之前的是dp【i】表示以第 i个元素结尾的最大子段和的值,我换了另一种,dp【i】表示前 i个元素的最大子段和的值(即,可以不包含p【i】,这个是在最大二子段和是用到的)。然后再判断过程中前求得起始位置,方法写了两种,都能A掉,我写在一起了发出来看。

代码如下:

1、AC代码:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int p[100010];
    int dp[100010];
    int t,n;
    int cnt=1;
    cin>>t;
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        cin>>n;
        int sum=0;
        int maxx=-11111;
        dp[0]=-10010;
        int left=1,right=n;
        int l=1;
        
        //方法一:
        for(int i=1;i<=n;i++)
        {
            cin>>p[i];
            sum+=p[i];
            if(sum>maxx)
            {
                maxx=sum;
                left=l;
                right=i;
            }
            if(sum<0)
            {
                sum=0;
                l=i+1;
            }
        }
        cout<<"Case "<<cnt<<":"<<endl;
        cout<<maxx<<" "<<left<<" "<<right<<endl;


        //方法二:
        /*
        for(int i=1;i<=n;i++)
        {
            cin>>p[i];
            if(dp[i-1]>p[i]+sum)
            {
                dp[i]=dp[i-1];
            }
            else
            {
                dp[i]=p[i]+sum;
                left=l;
                right=i;
            }
            sum+=p[i];
            if(sum<0)
            {
                sum=0;
                l=i+1;
            }
        }
        cout<<"Case "<<cnt<<":"<<endl;
        cout<<dp[n]<<" "<<left<<" "<<right<<endl;
        */
        
        cnt++;
        if(t)
            cout<<endl;
    }


    return 0;
}


2、1003方法,未AC(希望有人看到时可以帮忙指出错误)

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int t,n,ca=0;
    cin>>t;
    int p[100010];
    int dp[100010][2]; //dp[][0]代表字段和  dp[][1]代表该出的首元素的下表
    while(t--)
    {
        ca++;
        cin>>n;
        memset(p,0,sizeof(p));
        memset(dp,0,sizeof(dp));
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            cin>>p[i];
            if(p[i]>=0)
                flag=1;
        }
        cout<<"Case "<<ca<<":"<<endl;
        if(flag==0)         //小优化,也是一步处理,如果都小于0的情况
        {


            cout<<"0 "<<1<<" "<<1<<endl;
            if(t)
                cout<<endl;
            continue;
        }
        if(p[1]>=0)
        {
            dp[1][0]=p[1];
            dp[1][1]=1;
        }
        else
        {
            dp[1][0]=0;
            dp[1][1]=2;
        }
        for(int i=2;i<=n;i++)
        {
            if(dp[i-1][0]+p[i]>=0) //如果满足条件
            {
                dp[i][0]=dp[i-1][0]+p[i]; //得到字段和
                dp[i][1]=dp[i-1][1];  //得到首元素下标  等于之前的
            }
            else  //反之
            {
                dp[i][0]=0;
                dp[i][1]=i+1;     //首元素坐标为下一个数的下标  因为 该数和之前的数都确定排除了,只能从下一个开始
            }
        }
        int maxx=0,cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(maxx<dp[i][0])
            {
                maxx=dp[i][0];
                cnt=i;
            }
        }
        cout<<dp[cnt][0]<<" "<<dp[cnt][1]<<" "<<cnt<<endl;
        if(t)
            cout<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值