可以先处理出每个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. SpringFramework_module

    org.springframework : spring-aop:基于代理的AOP spring-aspects:基于切面的AspectJ spring-beans:beans spring-cont ...

  2. uitabbarController tababr 上方横线隐藏

    在自定义的tabbarController里加上这一句就ok [[UITabBar appearance] setShadowImage:[UIImage new]]; [[UITabBar appe ...

  3. js replace(a,b)之替换字符串中所有指定字符的方法

    var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1); // 打印结果: obcadeacf var str2 = ...

  4. Yii2 基于RESTful api 坑 访问会把控制器名称变为复数

    'urlManager' => [ 'class' => 'yii\web\UrlManager', 'enablePrettyUrl' => true, 'enableStrict ...

  5. XSL相关

    <?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="tex ...

  6. 设计模式--命令模式(Command)

    基本概念:  Command模式也叫命令模式 ,是行为设计模式的一种.Command模式通过被称为Command的类封装了对目标对象的调用行为以及调用参数,命令模式将方法调用给封装起来了. 命令模式的 ...

  7. 第三课3、ROS的launch文件

    1.launch 参数说明: pkg为节点的功能包,type为需要运行的那个节点,name为节点的名字,后面的参数可选 举个例子: 在工作空间中新建一个包: 然后再回到工作空间中去编译: 再在包目录下 ...

  8. SOCKET的一些注意事项

    1. 如果采用同步处理,其实,是可以多次接受数据的, MemoryStream ms = new MemoryStream();    while (true)   {     Console.Wri ...

  9. c# listview的使用

    C#向listview中添加项

  10. 使用SQL 从表中取记录

    SQL 的主要功能之一是实现数据库查询. 你使用查询来取得满足特定条件的信息. 一个简单的表查询实例 SQL 查询的句法非常简单.假设有一个名为email_table 的表,包含名字和地址两个字段,要 ...