可以先处理出每个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-8;
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 = 0;while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }return x;
}const int maxn=50000+10;
int T,n,a[maxn],L[maxn],R[maxn],dp[maxn][30];void RMQ_init()
{for(int i=0;i<n;i++) dp[i][0]=i;for(int j=1;(1<<j)<=n;j++)for(int i=0;i+(1<<j)-1<n;i++){if(a[dp[i][j-1]]>a[dp[i+(1<<(j-1))][j-1]]) dp[i][j]=dp[i][j-1];else dp[i][j]=dp[i+(1<<(j-1))][j-1];}
}int RMQ(int L,int R)
{int k=0;while((1<<(k+1))<=R-L+1) k++;if(a[dp[L][k]]>a[dp[R-(1<<k)+1][k]]) return dp[L][k];return dp[R-(1<<k)+1][k];
}int main()
{scanf("%d",&T); int cas=1;while(T--){scanf("%d",&n);for(int i=0;i<n;i++) scanf("%d",&a[i]), L[i]=R[i]=i;for(int i=1;i<n;i++){if(a[i]<a[i-1]) continue; int pre=L[i-1];while(1) { L[i]=pre; if(pre==0||a[pre-1]>a[i]) break; pre=L[pre-1]; }}for(int i=n;i>=1;i--) R[i]=i;for(int i=n-2;i>=0;i--){if(a[i]<a[i+1]) continue; int pre=R[i+1];while(1) { R[i]=pre; if(pre==n-1||a[pre+1]>a[i]) break; pre=R[pre+1]; }}RMQ_init();printf("Case %d:\n",cas++);for(int i=0;i<n;i++){if(L[i]>i-1) printf("0 "); else printf("%d ",RMQ(L[i],i-1)+1);if(R[i]<i+1) printf("0\n"); else printf("%d\n",RMQ(i+1,R[i])+1);}}return 0;
}

转载于:https://www.cnblogs.com/zufezzt/p/5740703.html

HDU 3410 Passing the Message相关推荐

  1. HDU - 3410 Passing the Message 单调递减栈

    Passing the Message What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Su ...

  2. 单调栈 左右传消息 HDU 3410

    Passing the Message What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Su ...

  3. 【HDU - 3410 】 Passing the Message(单调栈)

    题干: What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Sun Flower" k ...

  4. Passing the Message(HDU-3410)

    Problem Description What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Su ...

  5. Passing the Message(单调栈)

    What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Sun Flower" kinde ...

  6. 【ZOJ - 2724】【HDU - 1509】Windows Message Queue(优先队列)

    题干: Message queue is the basic fundamental of windows system. For each process, the system maintains ...

  7. HDU 4300 Clairewd’s message

    一道KMP的变式 本题仍是求最大前缀后缀,所以仍用KMP,但不同的是,本题有一个密码转换规则,不过好在题目中说了两段不重合,那么我们就可以在中间插入一个特殊符号'*'',保证求next数组时不会越过中 ...

  8. HDU3410 Passing the Message 【单调栈】

    题意 一个小孩可以看到左边比他矮,但是最高的孩子,同样可以看到右边比他矮,但是最高的小孩 分析 求左边比他矮,但是最高的孩子:维护一个单调栈,从栈顶到栈底单调递增: 从左边第一个孩子开始,如果栈为空或 ...

  9. HDU - 4300 Clairewd’s message(扩展KMP)

    题目链接:点击查看 题目大意:给出两个字符串 s 和 t ,字符串 s 代表着一种密码的映射,字符串 t 代表着一段密文+明文,题目保证密文是完整的,但明文只有一部分,现在问如何补全字符串 t ,使得 ...

最新文章

  1. oracle误删scott文件如何恢复
  2. MATLAB从入门到精通-Matlab R2020b新功能 | 子标题和标题/标签对齐功能!
  3. mysql为什么采用b树_为什么MongoDB采用B树索引,而Mysql用B+树做索引
  4. 可以输入值的下拉框(select和input的组合使用)
  5. pythonjava解释xml_Python解析XML文档
  6. linux 高性能读书笔记之通用socket地址
  7. 在Nginx/Tengine服务器上安装SSL证书
  8. 炫酷流光个人主页源码分享及在线部署教程
  9. Springboot+vue项目疫情社区防控系统
  10. amtlib.dll被McAfee删除之后?
  11. Windows 7安装超级终端连接COM口设备
  12. logisim 数码管输出设计
  13. 大数据在智慧消防领域的应用
  14. Web前端开发中最基本的找错误的方法, 你得了解一下
  15. 硬件:Nand Flash、Nor Flash
  16. 如何带好一个20人团队?
  17. 【动网论坛7.1 sp1 修改】-加强检查注册昵称,防止全数字及简单重复ID注册
  18. windows XP系统文件诠释C(转)
  19. Hi-End音箱真伪(3)-巴汉著文
  20. python音乐可视化效果_Python 一个漂亮的音乐节奏可视化方案!我觉得可行!

热门文章

  1. Angular6自定义指令实现多图片上传预览
  2. python使用阿里云sdk
  3. 【docker】常用命令
  4. Navicat使用Instant Client创建连接到Oracle数据库的正确姿势
  5. Sonar问题解决:普通方法调用静态属性
  6. Lucene第一篇【介绍Lucene、快速入门】
  7. mongodb的增、删、改、插的一个实例
  8. swith语句的较安全用法
  9. 基于OpenStreetMap计算驾车距离(Java)
  10. 在虚拟机上安装Docker并运行Nginx