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

谁来看看,自己觉得都考虑全了(CODE)

Posted by JiangLY at 2005-09-10 18:05:57 on Problem 2593
var
  l,r,s:array[0..100000] of longint;
  i,max,n,sum:longint;
function fmax(a,b,c:longint):longint;
begin
  if (a>=b)and(a>=c) then fmax:=a
  else if (b>=a)and(b>=c) then fmax:=b
  else if (c>=a)and(c>=b) then fmax:=c;
end;
begin
  readln(n);
  while n<>0 do
    begin
      for i:=1 to n do read(s[i]);
      readln;
      fillchar(l,sizeof(l),0);
      fillchar(r,sizeof(r),0);
      l[0]:=0;
      l[1]:=s[1];
      r[n-1]:=s[n];
      r[n]:=0;
      sum:=s[1];
      for i:=2 to n do
        begin
          if sum<0 then
            begin
              sum:=s[i];
              l[i]:=s[i];
            end
                   else
            begin
              sum:=sum+s[i];
              l[i]:=sum;
            end;
        end;
      sum:=s[n];
      max:=sum;
      for i:=n-2 downto 0 do
        begin
          r[i]:=fmax(sum,s[i+1],sum+s[i+1]);
          if max>r[i] then r[i]:=max;
          if sum<0 then sum:=s[i+1]
                   else sum:=sum+s[i+1]; 
          if sum>max then max:=sum;
        end;
      max:=-maxlongint;
      for i:=0 to n do
        if l[i]+r[i]>max then max:=l[i]+r[i];
      writeln(max);
      readln(n);
    end;
end.

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