不是VIP会员,不能显示答案

题目解答

题目:
const NN = 111111;
var
a, pre, suf, st : array[0 .. NN] of integer;
n, i, x, cnt, ans : integer;
function max(x, y : integer) : integer;
begin
max := x;
if (x < y) then max := y;
end;
begin
readln(n);
for i := 1 to n do read(a[i]);
cnt := 1;
st[cnt] := n;
for i := n-1 downto 1 do
begin
x := a[i];
while (cnt <> 0) and (x >= a[st[cnt]]) do dec(cnt);
suf[i] := st[cnt];
inc(cnt);
st[cnt] := i;
end;
cnt := 1;
st[cnt] := 1;
for i := 2 to n do
begin
x := a[i];
while (cnt <> 0) and (x >= a[st[cnt]]) do dec(cnt);
pre[i] := st[cnt];
cnt :=cnt+1;
st[cnt] := i;
end;
for i := 1 to n do
ans := max(ans, pre[i] * suf[i]);
writeln(ans);
end.
输入:
5
5 4 3 4 5

输出:8
考点: 0
分析:
解答: 手工去模拟一下suf=(0,5,4,5,0),pre=(0,1,2,1,0),容易得出2*4=8最大。
评论:
老师: 0