约翰的N(1≤N≤105)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向左看齐.对于奶牛i,如果奶牛j满足i<j且Hi<Hj,我们可以说奶牛i可以仰望奶牛j. 求出每只奶牛离她最近的仰望对象.
标签:
题解:再一次请出我神奇的小线段树——用线段树实现求在某某位置之后大于某值的最靠左位置,用了一个比较神奇的分治,具体如程序(发现线段是是个乱搞神器啊有木有)
1 var 2 i,j,k,l,m,n:longint; 3 a,b:array[0..1000000] of longint; 4 function max(x,y:longint):longint;inline; 5 begin 6 if x>y then max:=x else max:=y; 7 end; 8 function min(x,y:longint):longint;inline; 9 begin 10 if x<y then min:=x else min:=y; 11 end; 12 procedure built(z,x,y:longint);inline; 13 begin 14 if x=y then 15 begin 16 read(a[z]); 17 b[x]:=a[z]; 18 end 19 else 20 begin 21 built(z*2,x,(x+y) div 2); 22 built(z*2+1,(x+y) div 2+1,y); 23 a[z]:=max(a[z*2],a[z*2+1]); 24 end; 25 end; 26 function approach(z,x,y,l,r,t:longint):longint;inline; 27 var a1:longint; 28 begin 29 if l>r then exit(0); 30 if a[z]<=t then exit(0); 31 if x=y then 32 begin 33 if a[z]>t then exit(x); 34 end; 35 a1:=approach(z*2,x,(x+y) div 2,l,min(r,(x+y) div 2),t); 36 if a1<>0 then exit(a1); 37 exit(approach(z*2+1,(x+y) div 2+1,y,max((x+y) div 2+1,l),r,t)); 38 end; 39 begin 40 readln(n); 41 built(1,1,n); 42 for i:=1 to n do 43 writeln(approach(1,1,n,i+1,n,b[i])); 44 45 end.
3401: [Usaco2009 Mar]Look Up 仰望
标签:
原文地址:http://www.cnblogs.com/HansBug/p/4395050.html