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

    Atomic 采用类似github的版本管理,  可以使用以下命令升级 ostree remote add --set=gpg-verify=false atomic20160212 http://. ...

  2. flask安装首页显示

    参考:http://flask.pocoo.org/1.安装和测试[root@node1 flask]#pip install flaskd[root@node1 flask]# cat app.py ...

  3. sql server 查询表基本信息sql

    SELECT c.name,t.name TYPE,c.max_length,c.precision,c.scale,p.value FROM sys.systypes t INNER JOIN sy ...

  4. JAVA中方法的定义与使用(课堂学习归纳)

    组合数,百度词条是这样解释的:从m个不同元素中取出n(n≤m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数(Combination) 对于计算组合数,需要一定的工作量,计算机可以很 ...

  5. java 生成证书用于https

    在jdk的bin目录下运行: keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore " ...

  6. instanceof运算符、Class的isInstance( )与isAssignableFrom之间的区别

    instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false.形象地:自身实例或子类实例 instance ...

  7. ajaxUploadFile在IE9等IE浏览器,上传变json下载的问题解决(SpringMVC + ajaxUploadFile)

    使用ajaxUploadFIle进行文件上传,在google中正常,但是IE中却变成了json文件下载.经过搜索和尝试,解决方案如下: 首先明确,该功能使用$.ajaxFileUpload而非ajax ...

  8. ThreadLocal线程本地变量

    首先说明ThreadLocal存放的值是线程内共享的,线程间互斥的,主要用于线程内共享一些数据,避免通过参数来传递,这样处理后,能够优雅的解决一些实际问题,比如hibernate中的OpenSessi ...

  9. 腾讯应用开发3006 : name lookup timed out 错误

    昨天到今天发现应用访问不正常,用户发表数据很少,一查日志,QQ的 openApi 返回的结果全是 name lookup timed out,莫名其妙. 在服务器上 ping , nslookup , ...

  10. WITH common_table_expression

    Feature: 公用表表达式只能包含一个SELECT,多SELECT需UNION,UNION ALL 公用表表达式只能引用一次 公用表表达式可以包括对自身的引用,这种表达式称为递归公用表表达式 -- ...