3401: [Usaco2009 Mar]Look Up 仰望

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.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏HansBug's Lab

1635: [Usaco2007 Jan]Tallest Cow 最高的牛

1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Sub...

33750
来自专栏HansBug's Lab

2020: [Usaco2010 Jan]Buying Feed, II

2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MB Subm...

28870
来自专栏HansBug's Lab

1688: [Usaco2005 Open]Disease Manangement 疾病管理

1688: [Usaco2005 Open]Disease Manangement 疾病管理 Time Limit: 5 Sec  Memory Limit: ...

30850
来自专栏HansBug's Lab

1707: [Usaco2007 Nov]tanning分配防晒霜

1707: [Usaco2007 Nov]tanning分配防晒霜 Time Limit: 5 Sec  Memory Limit: 64 MB Submit:...

30770
来自专栏HansBug's Lab

1664: [Usaco2006 Open]County Fair Events 参加节日庆祝

1664: [Usaco2006 Open]County Fair Events 参加节日庆祝 Time Limit: 5 Sec  Memory Limit:...

27550
来自专栏HansBug's Lab

1753: [Usaco2005 qua]Who's in the Middle

1753: [Usaco2005 qua]Who's in the Middle Time Limit: 5 Sec  Memory Limit: 64 MB ...

342110
来自专栏HansBug's Lab

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MB...

31470
来自专栏HansBug's Lab

1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec  Memory Limit: 64 M...

29860
来自专栏HansBug's Lab

1724: [Usaco2006 Nov]Fence Repair 切割木板

1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Su...

30670
来自专栏HansBug's Lab

1653: [Usaco2006 Feb]Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MB ...

28540

扫码关注云+社区

领取腾讯云代金券