bzoj 3401 && bzoj 1657 单调栈

来源:互联网 发布:淘宝联盟怎么生成图文 编辑:程序博客网 时间:2024/04/30 08:09

题意:

bzoj 3401 :对于每一个i,找到离它最近的j满足 i<j 且hi<hj,对于每个i输出对应的j,如果没有满足的j输出0

bzoj 1657 :给定n个高度和音量。对于每一个i,找到离它最近的j1、j2满足 i<j1 且hi<hj1 或 j2<i 且 hi<hj2 ,把i的音量加到j1、j2的答案中,求max{ans[i]}


bzoj 3401 倒序单调栈

type        rec=record            h,num:longint;end;var        n,top           :longint;        i               :longint;        a,ans           :array[0..100010] of longint;        z               :array[0..100010] of rec;begin   read(n);   for i:=1 to n do read(a[i]);   top:=0;   for i:=n downto 1 do   begin      while (top<>0) and (z[top].h<=a[i]) do dec(top);      if top=0 then ans[i]:=0 else ans[i]:=z[top].num;      inc(top);      z[top].h:=a[i]; z[top].num:=i;   end;   for i:=1 to n do writeln(ans[i]);end.


bzoj 1657 正反两遍单调栈

type        rec=record            h,num:longint;end;var        n,top           :longint;        maxn            :int64;        z               :array[0..50010] of rec;        h,v             :array[0..50010] of longint;        ans             :array[0..50010] of int64;        i               :longint;begin   read(n);   for i:=1 to n do read(h[i],v[i]);   top:=0;   //   for i:=1 to n do   begin      while (top<>0) and (z[top].h<=h[i]) do dec(top);      if top<>0 then inc(ans[z[top].num],int64(v[i]));      inc(top);      z[top].h:=h[i]; z[top].num:=i;   end;   //   top:=0;   for i:=n downto 1 do   begin      while (top<>0) and (z[top].h<=h[i]) do dec(top);      if top<>0 then inc(ans[z[top].num],int64(v[i]));      inc(top);      z[top].h:=h[i];  z[top].num:=i;   end;   maxn:=0;   for i:=1 to n do if ans[i]>maxn then maxn:=ans[i];   writeln(maxn);end.

——by Eirlys



0 0
原创粉丝点击