可以先处理出每个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的更多相关文章

  1. hdu 3410 Passing the Message(单调队列)

    题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...

  2. 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 ...

  3. hdu 3410 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...

  4. Passing the Message

    Passing the Message http://acm.hdu.edu.cn/showproblem.php?pid=3410 Time Limit: 2000/1000 MS (Java/Ot ...

  5. HDU 3410 &amp;&amp; POJ 3776 Passing the Message 单调队列

    题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...

  6. hdu 4300 Clairewd’s message KMP应用

    Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...

  7. Hdu 3410 【单调队列】.cpp

    题意: 给出一个数组,问你对于第i个数,从最后一个比它大的数到它之间比它小的数中最大的那个数的下标,以及它右边到第一个比它大的数中比它小的数中最大的那一个数的下标<下标从1开始>. eg: ...

  8. Passing the Message 单调栈两次

    What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...

  9. hdu 4119 Isabella's Message 模拟题

    Isabella's Message Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

随机推荐

  1. something funny

    something funny. #include <stdio.h> #include <windows.h> #define N 50 HANDLE hConsole; v ...

  2. dict字典学习笔记

    dict的创建:                            #第一行初始化指定一个dict d = {'1':'刘刚','2':李萌,'3':89}  #key在前,value在后.key ...

  3. 第一个Android crackme(2016-05)

    第一个Android crackme 0x00 背景 最近在学习Android的逆向,把基本的环境搭好后,看了看<第一行代码--Android>,然后就按照非虫大牛的<Android ...

  4. 向量空间(Vector Spaces)

    向量空间(Vector Spaces) 向量空间又称线性空间,是线性代数的中心内容和基本概念之一.在解析几何里引入向量的概念后,是许多问题的处理变得更为简洁和清晰,在此基础上的进一步抽象化,形成了与域 ...

  5. try{}catch{}finally{}的手记

    try{ System.out.println("执行try"); int = 6 / 0; return 1; }catch(Exception e){ System.out.p ...

  6. Spring的监听器ContextLoaderListener

    一.作用 ContextLoaderListener监听器的作用就是启动web容器时,自动装配ApplicationContext的配置信息.它实现了ServletContextListener接口, ...

  7. ios打开系统自带APP

    打开系统自带app 打开系统设置: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root= ...

  8. Date 和 SimpleDateFormat 类表示时间

    Date now=new Date(); // 使用format()方法将日期转换为指定格式的文本 SimpleDateFormat sdf1 = new SimpleDateFormat(" ...

  9. 《JS权威指南学习总结--6.3删除属性》

    内容要点: 一.delete运算符可以删除对象的属性.它的操作数应当是一个属性访问表达式.让人感到意外的是,delete只是断开属性和宿主对象的联系,而不会去操作属性中的属性: delete book ...

  10. Spring MVC Flash Attribute

    转自:Spring MVC Flash Attribute 的讲解与使用示例 Spring MVC 3.1版本加了一个很有用的特性,Flash属性,它能解决一个长久以来缺少解决的问题,一个POST/R ...