`
lovnet
  • 浏览: 6693203 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

题目要求两段子序列和

分段就行 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.


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics