前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >3401: [Usaco2009 Mar]Look Up 仰望

3401: [Usaco2009 Mar]Look Up 仰望

作者头像
HansBug
发布2018-04-10 17:07:08
6880
发布2018-04-10 17:07:08
举报
文章被收录于专栏:HansBug's LabHansBug's Lab

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

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

代码语言:javascript
复制
 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.
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-04-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 3401: [Usaco2009 Mar]Look Up 仰望
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档