# P3401: [Usaco2009 mar]look up

Source: Internet
Author: User

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

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.