The first thought of this problem is a tree-like array, so it is possible to fight
1 Constmaxn=4000001;2 varN,i,j,maxx:longint;3h,l:array[0..400001] of Longint;4p:array[0..4000000] of Longint;5 function Lowbit (x:longint): Longint;6 begin7Exit (x and (-x));8 end;9 procedure Insert (x,i:longint);Ten varTem:longint; One begin Atem:=x; - whiletem<=maxx+1 Do - begin thep[tem]:=i; -tem:=tem+lowbit (TEM); - end; - end; + function Work (x:longint): Longint; - varTem:longint; + begin Atem:=MAXN; at whileX>0 Do - begin - if(P[x]<tem) and (p[x]<>0) then tem:=P[x]; -x:=x-lowbit (x); - end; - ifTem=maxn Then Exit (0) in Elseexit (TEM); - end; to begin + READLN (n); - fori:=1to n Do the begin * readln (H[i]); $ ifH[i]>maxx then maxx:=H[i];Panax Notoginseng end; - fori:=1to n Do theh[i]:=maxx+1-H[i]; +l[n]:=0; A Insert (h[n],n); the fori:=n-1Downto1 Do + begin -L[i]:=work (h[i]-1); $ Insert (h[i],i); $ end; - fori:=1to n1 Do - Writeln (L[i]); the write (L[n]); -End.
However, the correct solution to this problem is a monotonous stack!!
1 type2Node=Record3 Num,h:longint;4 end;5 varN,i,j,num,now:longint;6stack:array[0..100001] of node;7h,l:array[0..100001] of Longint;8 begin9 READLN (n);Ten fori:=1to n Do One readln (H[i]); Anow:=0; - forI:=n Downto1 Do - begin the while(now>0) and (Stack[now].h<=h[i]) Do - Dec (now); -l[i]:=Stack[now].num; - Inc (now); +stack[now].h:=H[i]; -stack[now].num:=i; + end; A fori:=1to n DoWriteln (L[i]); atEnd.
All right, almost, almost =-=//.
P3401: [Usaco2009 mar]look up