Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么我的还是总WA?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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator