Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

大神帮忙看看 超时了

Posted by 268123lry at 2015-01-30 18:52:05 on Problem 2479
#include<iostream>
#include<cstdio>
#define max 50000
using namespace std;
int maxsum(int *a,int head,int tail)
{
    int sum=-999999;
    int b=0;
    for(int i=head;i<=tail;i++)
    {
        if(b>0) b+=a[i];
        else b=a[i];
        if(b>sum) sum=b;
    }
    return sum;
}
int main()
{
    int n;
    int T;
    int a[max+1];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",a+i);
        int sum=-999999999;
        int result;
        for(int i=1;i<=n;i++)
        {
            result=maxsum(a,1,i)+maxsum(a,i+1,n);
            if(result>=sum)
            {
                sum=result;
            }
        }
        printf("%d\n",sum);
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator