What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten are prepared to have an excursion. Before kicking off, teacher Liu tells them to stand in a row. Teacher Liu has an important message to announce, but she doesn’t want to tell them directly. She just wants the message to spread among the kids by one telling another. As you know, kids may not retell the message exactly the same as what they was told, so teacher Liu wants to see how many versions of message will come out at last. With the result, she can evaluate the communication skills of those kids.
Because all kids have different height, Teacher Liu set some message passing rules as below:

1.She tells the message to the tallest kid.

2.Every kid who gets the message must retell the message to his “left messenger” and “right messenger”.

3.A kid’s “left messenger” is the kid’s tallest “left follower”.

4.A kid’s “left follower” is another kid who is on his left, shorter than him, and can be seen by him. Of course, a kid may have more than one “left follower”.

5.When a kid looks left, he can only see as far as the nearest kid who is taller than him.

The definition of “right messenger” is similar to the definition of “left messenger” except all words “left” should be replaced by words “right”.

For example, suppose the height of all kids in the row is 4, 1, 6, 3, 5, 2 (in left to right order). In this situation , teacher Liu tells the message to the 3rd kid, then the 3rd kid passes the message to the 1st kid who is his “left messenger” and the 5th kid who is his “right messenger”, and then the 1st kid tells the 2nd kid as well as the 5th kid tells the 4th kid and the 6th kid.
Your task is just to figure out the message passing route.


InputThe first line contains an integer T indicating the number of test cases, and then T test cases follows.
Each test case consists of two lines. The first line is an integer N (0< N <= 50000) which represents the number of kids. The second line lists the height of all kids, in left to right order. It is guaranteed that every kid’s height is unique and less than 2^31 – 1 .OutputFor each test case, print “Case t:” at first ( t is the case No. starting from 1 ). Then print N lines. The ith line contains two integers which indicate the position of the ith (i starts form 1 ) kid’s “left messenger” and “right messenger”. If a kid has no “left messenger” or “right messenger”, print ‘0’ instead. (The position of the leftmost kid is 1, and the position of the rightmost kid is N)Sample Input

2
5
5 2 4 3 1
5
2 1 4 3 5

Sample Output

Case 1:
0 3
0 0
2 4
0 5
0 0
Case 2:
0 2
0 0
1 4
0 0
3 0题意 是输出每个左边(右边)比他矮的 且最高的人的高度  也就是向左(向右)找到第一个比他高的 然后他到这个人之间的最高的高度 如果没有 输出0 左右各来一遍单调栈 用flag判断是否存在代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>#define MAXN 50005using namespace std;struct D{int a,b;
};int board[MAXN];
int r[MAXN];
int l[MAXN];
struct D s[MAXN];int digit;int main(){int T;scanf("%d",&T);while(T--){int N;scanf("%d",&N);for(int i=1 ; i<=N ; i++){scanf("%d",&board[i]);}int top = -1;memset(r,0,sizeof(r));memset(l,0,sizeof(l));for(int i=1 ; i<=N  ; i++){if(top == -1){s[++top].a = board[i];s[top].b = i;}else{int max = 0;int tt;while(s[top].a<=board[i] && top>=0){top--;if(s[top+1].a>max && s[top+1].a!=board[i]){max = s[top+1].a;tt = s[top+1].b;}}if(!max)l[i] = 0;else l[i] = tt;s[++top].a = board[i];s[top].b  = i;} }top = -1;for(int i=N ; i>=1  ; i--){if(top == -1){s[++top].a = board[i];s[top].b = i;}else{int max = 0;int tt;while(s[top].a<=board[i] && top>=0){top--;if(s[top+1].a>max && s[top+1].a!=board[i]){max = s[top+1].a;tt = s[top+1].b;}}if(max == 0)r[i] = 0;else r[i] = tt;s[++top].a = board[i];s[top].b = i;}  }printf("Case %d:\n",++digit);for(int i=1 ; i<=N ; i++){printf("%d %d\n",l[i],r[i]);}}return 0;
}

用单调递增栈做,栈内元素用结构体,同时存储高度和下标。

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 "Sun Flower" k ...

  3. HDU3410 Passing the Message 【单调栈】

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

  4. 线性结构 —— 单调栈与单调队列

    [单调栈] 1.原理 单调栈,就是栈内元素保持一定单调性(单调递增或单调递减)的栈,即从栈底到栈顶单调递增或递减. 对于单调递增的栈,如果栈为空或入栈元素值大于等于栈顶元素值,则入栈:否则,若入栈会破 ...

  5. 解题报告 (十) 单调栈

    文章目录 单调栈 解题报告 PKU 2082 Terrible Sets HDU 2430 Beans HDU 4252 A Famous City PKU 2796 Feel Good HDU 34 ...

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

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

  7. POJ2796 Feel Good(单调栈)

    题意: 给出一列数据,要求一个区间内最小值与区间内数据总和乘积最大值 要点: 还是单调栈,这次我自己写的,先做了几题比较简单的果然还是有效果的,这题也是一样,按点遍历,网上大神做的是直接遍历一次即可, ...

  8. 【单调栈 前缀和 异或】7.21序列求和

    还要再细细思考的奇妙思路 题目描述 小A最近喜欢上了关于区间max的问题.她定义一个区间的价值是max(ai)(l<=i<=r)∗(alxoral+1xor...xorar)max(ai) ...

  9. 栈与队列7——单调栈结构(进阶问题)

    题目 一个含有重复值的数组arr,找到每一个i位置左边和右边离i位置最近且值比arr[i]小的位置,返回所有相应的信息. 举例:arr={3,4,1,5,6,2,7},返回如下的二维数组作为结果:{{ ...

最新文章

  1. spring_快速提示:在Spring中引用其他属性
  2. 怎么查看python是否安装成功-如何查看python是否安装成功?
  3. 用java编写五子棋游戏_java编写一个五子棋游戏,拜托了
  4. linux下添加用户并赋予root权限
  5. iometer测试工具
  6. Fiddler使用方法简介
  7. 直播预告│据说这是人工智能领域里最难的一门学问……
  8. Error: Could not open client transport with JDBC Uri: jdbc:hive2://henu2:10000: java.n et.ConnectExc
  9. ECharts 雷达图在类目值下面显示数值
  10. # Schedulerx正式登陆Ali-k8s应用目录
  11. ubuntu安装python_常用linux指令 ubuntu环境pytorch配置
  12. Java基础篇:构造函数重载
  13. 20145324 20145325《信息安全系统设计基础》实验二 固件设计
  14. 华为 5680 OLT native vlan配置方式
  15. Linux之父:不担心微软接管Linux
  16. 如何关闭CSDN右下角的小广告
  17. CVPR2020(Enhancement):论文解读《Zero-Reference Deep Curve Estimation for Low-Light Image Enhancement》
  18. DSPE-PEG9-Mal纯度是95%以上的单分散小分子PEG试剂
  19. 睡眠排序算法c语言实现,Linux 进程必知必会
  20. navigationController的一些用法

热门文章

  1. 健康知识-健康四大基石(维多利亚宣言)
  2. 算法-母牛繁殖求N年后的总牛数量
  3. Linux 安装英伟达显卡驱动和CUDA套件 // Kali // Debian // Ubuntu
  4. FPGA 四画面视频分割逻辑框图
  5. java 溢出与泄露_Java内存溢出与栈溢出
  6. GDPU C语言 天码行空12
  7. C/C++ 心型绘制
  8. 巨头涌入,资本押注,脑机接口的风能吹起来吗?
  9. 热门国产安卓android平板推荐(500元-1000元,1000元-1500元)
  10. VideoCap Pro ActiveX控件,视频捕获和图像处理