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

为什么我的还是总WA?

Posted by jackie_wyx at 2007-03-11 12:41:33 on Problem 2479
In Reply To:Re:O(n) 的算法, 从第一个开始, 一直加, 一直取最大值, 出现负值归零 Posted by:jackie_wyx at 2007-03-09 16:17:12
#include <stdio.h>

main()
{
  int t,n,k,i,max,sum;
  int a[50002],b[50002],c[50002];
  
  scanf("%d",&t);
  for(k=1;k<=t;k++)
  {
    scanf("%d",&n);
    max=0;
    sum=0;
    for(i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
      sum+=a[i];
      if(sum<0) sum=0;
      else if(sum>max) max=sum;
      b[i]=max;
    }
    max=0;
    sum=0;
    for(i=n;i>=1;i--)
    {
      sum+=a[i];
      if(sum<0) sum=0;
      else if(sum>max) max=sum;
      c[i]=max;
    }
    max=0;
    for(i=1;i<n;i++)
      if(b[i]+c[i+1]>max) max=b[i]+c[i+1];
    printf("%d\n",max);
  }

  return(0);
}

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