POJ 2479(2段连续子序列和)

来源:互联网 发布:育才行知小学对口初中 编辑:程序博客网 时间:2024/06/05 10:59

题目要求两段子序列和

分段就行 O(n)

话说这回数组又开小了,居然提示Runtime Error


Program P2479;var   t,n,i,j,m,m2,p:longint;   a,b,c:array[0..50000] of longint;begin   read(t);   a[0]:=-100001;   while (t>0) do   begin      read(n);      j:=0;      m:=0;      for i:=1 to n do      begin         read(a[i]);         if a[i]<0 then inc(j);         if a[i]>a[m] then m:=i;      end;      if j>n-2 then      begin         p:=a[m];         m2:=0;         for i:=1 to n do if (i<>m) and (a[m2]<a[i]) then m2:=i;         inc(p,a[m2]);         writeln(p);      end      else      begin         p:=0;         m:=-100001;         for i:=1 to n do         begin            inc(p,a[i]);            if (p<0) then p:=0;            if m<p then m:=p;            b[i]:=m;         end;         p:=0;         m:=-100001;         for i:=n downto 1 do         begin            inc(p,a[i]);            if (p<0) then p:=0;            if m<p then m:=p;            c[i]:=m;         end;         m:=0;         for i:=1 to n-1 do            if m<b[i]+c[i+1] then m:=b[i]+c[i+1];         writeln(m);      end;      dec(t);   end;end.


原创粉丝点击