P3401: [Usaco2009 Mar]Look Up 仰望
这道题第一眼还以为是树状数组,于是乎打着打着也是能过的
const maxn=;
var n,i,j,maxx:longint;
h,l:array[..] of longint;
p:array[..] of longint;
function lowbit(x:longint):longint;
begin
exit(x and (-x));
end;
procedure insert(x,i:longint);
var tem:longint;
begin
tem:=x;
while tem<=maxx+ do
begin
p[tem]:=i;
tem:=tem+lowbit(tem);
end;
end;
function work(x:longint):longint;
var tem:longint;
begin
tem:=maxn;
while x> do
begin
if (p[x]<tem) and (p[x]<>) then tem:=p[x];
x:=x-lowbit(x);
end;
if tem=maxn then exit()
else exit(tem);
end;
begin
readln(n);
for i:= to n do
begin
readln(h[i]);
if h[i]>maxx then maxx:=h[i];
end;
for i:= to n do
h[i]:=maxx+-h[i];
l[n]:=;
insert(h[n],n);
for i:=n- downto do
begin
l[i]:=work(h[i]-);
insert(h[i],i);
end;
for i:= to n- do
writeln(l[i]);
write(l[n]);
end.
然而这道题正确解法是 单调栈!!
type
node=record
num,h:longint;
end;
var n,i,j,num,now:longint;
stack:array[..] of node;
h,l:array[..] of longint;
begin
readln(n);
for i:= to n do
readln(h[i]);
now:=;
for i:=n downto do
begin
while (now>) and (stack[now].h<=h[i]) do
dec(now);
l[i]:=stack[now].num;
inc(now);
stack[now].h:=h[i];
stack[now].num:=i;
end;
for i:= to n do writeln(l[i]);
end.
好吧,差不多,差不多=-=//
(转载请注明出处:http://www.cnblogs.com/Kalenda/)
P3401: [Usaco2009 Mar]Look Up 仰望的更多相关文章
- BZOJ3401: [Usaco2009 Mar]Look Up 仰望
3401: [Usaco2009 Mar]Look Up 仰望 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 87 Solved: 58[Submit ...
- BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )
n <= 105 , 其实是10 ^ 5 ....坑...我一开始写了个模拟结果就 RE 了.. 发现这个后写了个单调栈就 A 了... ---------------------------- ...
- 3401: [Usaco2009 Mar]Look Up 仰望
3401: [Usaco2009 Mar]Look Up 仰望 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 136 Solved: 81[Submi ...
- 【BZOJ】3401: [Usaco2009 Mar]Look Up 仰望(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=3401 还能更裸一些吗.. 维护一个递减的单调栈 #include <cstdio> #i ...
- BZOJ 3401: [Usaco2009 Mar]Look Up 仰望(离线+平衡树)
刷银组刷得好开心= = 离线按权值排序,从大到小插入二叉树,查找树中比这个数大的 CODE: #include<cstdio>#include<iostream>#includ ...
- bzoj 3401: [Usaco2009 Mar]Look Up 仰望【单调栈】
用单调递减的栈从后往前扫一遍即可 #include<iostream> #include<cstdio> using namespace std; const int N=10 ...
- bzoj 3399: [Usaco2009 Mar]Sand Castle城堡
3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec Memory Limit: 128 MB Description 约翰用沙子建了一座城堡.正 ...
- BZOJ3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队
3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 89 Solve ...
- BZOJ3399: [Usaco2009 Mar]Sand Castle城堡
3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 22 Solved: 17[Sub ...
随机推荐
- C# 通过this关键字来扩展方法
好处:不需要继承,对现有类型进行扩展 public static class ExtString { public static string myTest(this String str) { re ...
- Button的图像位置设置
[button setImageEdgeInsets:UIEdgeInsetsMake(2, 70, 2,70 )]; [button setImageEdgeInsets:UIEdgeInsetsM ...
- PHP总结
1.PHP的简介: PHP(外文名:PHP: Hypertext Preprocessor,中文名:"超文本预处理器") 是一种通用开源脚本语言.语法吸收了C语言.Java和Per ...
- Git一张图学习
- Miniprofiler在普通net项目中的使用
1.Global.asax中配置 Void Application_BeginRequest(Object sender, EventArgs e){ If(Request.IsLocal){ //请 ...
- sqoop的job工具
sqoop job: Work with saved jobs 就是将sqoop的某条语句保存为一个job 1.把person表导入到HDFS上,可以使用下面的语句 sqoop import --co ...
- Azkaban遇到的坑-installation Failed.Error chunking
在使用azkaban做spark作业调度时,在上传zip包时报installation Failed.Error chunking错误,原来是于我们所编写的应用会上传到 MySQL 存储,过大的zip ...
- Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法
前言 我在实验进入linux系统启动xwindow server而不启动KDE GNOME等桌面系统时遇到的问题.只启动x server而不启动桌面系统,在xserver之上运行一个全屏的图形界面程序 ...
- Ubuntu通过APT配置开发环境
apt-get install vim apt-get install ssh apt-get install apache2 apt-get install redis-server apt-get ...
- 最新hosts,更新hosts,可用
点击这里,全选后复制,粘贴到C:\Windows\System32\drivers\etc的hosts里面,把原来的置换了