标签:
这道题第一眼还以为是树状数组,于是乎打着打着也是能过的
1 const maxn=4000001;
2 var n,i,j,maxx:longint;
3 h,l:array[0..400001] of longint;
4 p:array[0..4000000] of longint;
5 function lowbit(x:longint):longint;
6 begin
7 exit(x and (-x));
8 end;
9 procedure insert(x,i:longint);
10 var tem:longint;
11 begin
12 tem:=x;
13 while tem<=maxx+1 do
14 begin
15 p[tem]:=i;
16 tem:=tem+lowbit(tem);
17 end;
18 end;
19 function work(x:longint):longint;
20 var tem:longint;
21 begin
22 tem:=maxn;
23 while x>0 do
24 begin
25 if (p[x]<tem) and (p[x]<>0) then tem:=p[x];
26 x:=x-lowbit(x);
27 end;
28 if tem=maxn then exit(0)
29 else exit(tem);
30 end;
31 begin
32 readln(n);
33 for i:=1 to n do
34 begin
35 readln(h[i]);
36 if h[i]>maxx then maxx:=h[i];
37 end;
38 for i:=1 to n do
39 h[i]:=maxx+1-h[i];
40 l[n]:=0;
41 insert(h[n],n);
42 for i:=n-1 downto 1 do
43 begin
44 l[i]:=work(h[i]-1);
45 insert(h[i],i);
46 end;
47 for i:=1 to n-1 do
48 writeln(l[i]);
49 write(l[n]);
50 end.
然而这道题正确解法是 单调栈!!
1 type
2 node=record
3 num,h:longint;
4 end;
5 var n,i,j,num,now:longint;
6 stack:array[0..100001] of node;
7 h,l:array[0..100001] of longint;
8 begin
9 readln(n);
10 for i:=1 to n do
11 readln(h[i]);
12 now:=0;
13 for i:=n downto 1 do
14 begin
15 while (now>0) and (stack[now].h<=h[i]) do
16 dec(now);
17 l[i]:=stack[now].num;
18 inc(now);
19 stack[now].h:=h[i];
20 stack[now].num:=i;
21 end;
22 for i:=1 to n do writeln(l[i]);
23 end.
好吧,差不多,差不多=-=//
P3401: [Usaco2009 Mar]Look Up 仰望
标签:
原文地址:http://www.cnblogs.com/Kalenda/p/4816767.html