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

Re:大神帮忙看看 超时了

Posted by 268123lry at 2015-01-30 18:52:21 on Problem 2479
In Reply To:大神帮忙看看 超时了 Posted by:268123lry at 2015-01-30 18:52:05
> #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);
>     }
> }先对数字串从左向右依次求出每段的连续子序列的最大字段和,并将其存入数组array[i]中(i为对应位置),再从右向左用同样的方法求一次最大字段和,并将每个子段i~n的和与对应的另一半1~i-1相加,求出最大值。也就是对每个位置i来说,求出[1~i-1]的最大子段和以及[i~n]的最大子段和,再相加起来,求最大的一个就行了。

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