(双子序列最大和)给定一个长度为 n(3 ≤ n ≤ 1000)的整数序列,要求从中选出两个连续子序列,使得这两个连续子序列的序列和之和最大,最终只需输出这个最大和。一个连续子序列的序列和为该连续子序列中所有数之和。要求:每个连续子序列长度至少为 1,且两个连续子序列之间至少间隔 1 个数。(第五空 4 分,其余 2.5 分)
const MAXN = 1000; var n, i, ans, sum : longint; x : array [1..MAXN] of longint; lmax : array [1..MAXN] of longint; // lmax[i]为仅含 x[i]及 x[i]左侧整数的连续子序列的序列和中,最大的序列和 rmax : array [1..MAXN] of longint; // rmax[i]为仅含 x[i]及 x[i]右侧整数的连续子序列的序列和中,最大的序列和 begin read(n); for i := 1 to n do read(x[i]); lmax[1] := x[1]; for i := 2 to n do if lmax[i - 1] <= 0 then lmax[i] := x[i] else lmax[i] := lmax[i - 1] + x[i]; for i := 2 to n do if lmax[i] < lmax[i - 1] then lmax[i] := lmax[i - 1]; 1; for i := n - 1 downto 1 do if rmax[i + 1] <= 0 then 2 else 3; for i := n - 1 downto 1 do if rmax[i] < rmax[i + 1] then 4; ans := x[1] + x[3]; for i := 2 to n - 1 do begin sum := 5; if sum > ans then ans := sum; end; writeln(ans); end.
