pku 2479 and pku 2593(DP)

http://hi.baidu.com/raulliubo/blog/item/a2b321c7715109ded1006088.html

 

这两个题是如此的相似,除了数据范围。

意思就是把一个序列从中间分成两半,使这两半中的两个片段的和的和最大。

做法是这样的,前从前面扫一遍,dp求从第i个位置分,前i的最大和。然后再从后面扫一遍,求出从第i的位置分,[i+1,n]的最大和,同时更新最大值。

附2593程序:

--------------------------------------------------------好帅的分割线------------------------------------------------------

var front,back:array[0..100001] of longint;
num:array[1..100001] of integer;
sum,now,n,i,j,last:longint;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;

begin
readln(n);
while n<>0 do begin
front[0]:=-1000000;
back[n+1]:=front[0];
last:=front[0];
sum:=front[0];
for i:=1 to n do begin
   read(num[i]);
   now:=max(num[i],num[i]+last);
   front[i]:=max(front[i-1],now);
   last:=now;
end;
for i:=n downto 1 do begin
   back[i]:=max(back[i+1]+num[i],num[i]);
   if back[i]+front[i-1]>sum then sum:=back[i]+front[i-1];
end;
writeln(sum);
readln(n);
end;
end.

posted @ 2009-01-04 12:35  jesonpeng  阅读(277)  评论(0编辑  收藏  举报