还是单调栈,维护递减即可,在进行计算,注意要保存前一个比当前数大的数之前有几个比它小。
1 var n,i,j,num,now,tem:longint; 2 ans:int64; 3 stack:array[0..1000001] of longint; 4 h,l:array[0..1000001] of longint; 5 begin 6 readln(n); 7 for i:=1 to n do 8 readln(h[i]); 9 now:=0; 10 ans:=0; 11 //fillchar(l,sizeof(l),1); 12 for i:=1 to n do 13 l[i]:=1; 14 for i:=n downto 1 do 15 begin 16 while (h[i]>stack[now]) and (now<>0) do dec(now); 17 inc(now); 18 stack[now]:=h[i]; 19 ans:=ans+n+1-i-now-l[now-1]; 20 l[now]:=n+1-i-now; 21 end; 22 write(ans); 23 end.
时间: 2024-12-12 01:36:47