HDU 3410 Passing the Message
可以先处理出每个a[i]最左和最右能到达的位置,L[i],和R[i]。然后就只要询问区间[ L[i],i-1 ]和区间[ i+1,R[i] ]最大值位置即可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=+;
int T,n,a[maxn],L[maxn],R[maxn],dp[maxn][]; void RMQ_init()
{
for(int i=;i<n;i++) dp[i][]=i;
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<n;i++)
{
if(a[dp[i][j-]]>a[dp[i+(<<(j-))][j-]]) dp[i][j]=dp[i][j-];
else dp[i][j]=dp[i+(<<(j-))][j-];
}
} int RMQ(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
if(a[dp[L][k]]>a[dp[R-(<<k)+][k]]) return dp[L][k];
return dp[R-(<<k)+][k];
} int main()
{
scanf("%d",&T); int cas=;
while(T--)
{
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&a[i]), L[i]=R[i]=i;
for(int i=;i<n;i++)
{
if(a[i]<a[i-]) continue; int pre=L[i-];
while() { L[i]=pre; if(pre==||a[pre-]>a[i]) break; pre=L[pre-]; }
}
for(int i=n;i>=;i--) R[i]=i;
for(int i=n-;i>=;i--)
{
if(a[i]<a[i+]) continue; int pre=R[i+];
while() { R[i]=pre; if(pre==n-||a[pre+]>a[i]) break; pre=R[pre+]; }
} RMQ_init();
printf("Case %d:\n",cas++);
for(int i=;i<n;i++)
{
if(L[i]>i-) printf("0 "); else printf("%d ",RMQ(L[i],i-)+);
if(R[i]<i+) printf("0\n"); else printf("%d\n",RMQ(i+,R[i])+);
}
}
return ;
}
HDU 3410 Passing the Message的更多相关文章
- hdu 3410 Passing the Message(单调队列)
题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...
- HDU - 3410 Passing the Message 单调递减栈
Passing the Message What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flo ...
- hdu 3410 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...
- Passing the Message
Passing the Message http://acm.hdu.edu.cn/showproblem.php?pid=3410 Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 3410 && POJ 3776 Passing the Message 单调队列
题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...
- hdu 4300 Clairewd’s message KMP应用
Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...
- Hdu 3410 【单调队列】.cpp
题意: 给出一个数组,问你对于第i个数,从最后一个比它大的数到它之间比它小的数中最大的那个数的下标,以及它右边到第一个比它大的数中比它小的数中最大的那一个数的下标<下标从1开始>. eg: ...
- Passing the Message 单调栈两次
What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...
- hdu 4119 Isabella's Message 模拟题
Isabella's Message Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...
随机推荐
- dict字典学习笔记
dict的创建: #第一行初始化指定一个dict d = {'1':'刘刚','2':李萌,'3':89} #key在前,value在后.key ...
- sql convert() 函数
convert: 时间格式转换为其他时间格式的函数 CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) data_type: ...
- python demo整理
1 变量作用域 #!/usr/bin/python # coding=utf-8 name = "whole global name" class Person: name = & ...
- JPA 系列教程 异常 集锦
异常1.hibernate升级到3.5版本 异常信息摘要: Associations marked as mappedBy must not define database mappings like ...
- mysql 查询语句
查询一段时间到另一段时间的数据 $from=$_POST['from']; $to=$_POST['to']; $query_xiangmu ="SELECT * FROM yichang ...
- JVM内存模型,垃圾回收算法
JVM内存模型总体架构图 程序计数器多线程时,当线程数超过CPU数量或CPU内核数量,线程之间就要根据时间片轮询抢夺CPU时间资源.因此每个线程有要有一个独立的程序计数器,记录下一条要运行的指令.线程 ...
- KVM下windows虚拟机使用virtio驱动
KVM下windows虚拟机默认disk使用的是Qemu IDE硬盘,网卡默认是rtl8139网卡.为了使kvm主机在相同的配置下,有更好的效率,可以将网卡和磁盘替换成virtio的驱动. windo ...
- Busy Beavers(暴力模拟)
由于排版问题,题目无法显示,可以到 http://7xjob4.com1.z0.glb.clouddn.com/e4872a15819b6bf9d1e5250bacc2a30b 查看 题目大意是有只 ...
- angular指令
转自:http://www.cnblogs.com/rohelm/p/4051437.html 对于指令,可以把它简单的理解成在特定DOM元素上运行的函数,指令可以扩展这个元素的功能. 首先来看个完整 ...
- ping 计算机全名,返回的不是IP地址
今天想看一下机子的IP地址,结果关闭局域防火墙后,在命令行中使用ping 计算机全名,返回的不是IP地址 其实,这也是一种IP地址,IP6地址 原因:默认情况下,win7以上的操作系统,ping 计算 ...