3401: [Usaco2009 Mar]Look Up 仰望

时间:2022-05-08
本文章向大家介绍3401: [Usaco2009 Mar]Look Up 仰望,主要内容包括3401: [Usaco2009 Mar]Look Up 仰望、Description、Input、Output、Sample Input、Sample Output、HINT、Source、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

3401: [Usaco2009 Mar]Look Up 仰望

Time Limit: 3 Sec  Memory Limit: 128 MB

Submit: 136  Solved: 81

[Submit][Status][Discuss]

Description

约翰的N(1≤N≤105)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向左看齐.对于奶牛i,如果奶牛j满足i<j且Hi<Hj,我们可以说奶牛i可以仰望奶牛j.    求出每只奶牛离她最近的仰望对象.

Input

    第1行输入N,之后每行输入一个身高.

Output

    共N行,按顺序每行输出一只奶牛的最近仰望对象.如果没有仰望对象,输出0.

Sample Input

6 3 2 6 1 1 2

Sample Output

3 3 0 6 6 0

HINT

Source

Silver

题解:再一次请出我神奇的小线段树——用线段树实现求在某某位置之后大于某值的最靠左位置,用了一个比较神奇的分治,具体如程序(发现线段是是个乱搞神器啊有木有)

 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.