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. 露雨资源库(第一个.net2.0软件)二
  2. oracle导入导出命令
  3. 皖南医学院2020C语言试卷,安徽继续教育在线 - 皖南医学院
  4. java保持运行_保持Java程序无限运行的方法是有效的吗?
  5. Java中局部变量必须初始化
  6. iOS CGRectGetMaxX/Y 使用
  7. 信息学奥赛一本通 1312:【例3.4】昆虫繁殖
  8. java读取本地图片的绝对地址_我想使用图片的绝对路径。为什么java不能使用图片的绝对路径。...
  9. RouterOS 5.22固定公网IP共享上网设置
  10. [JNI] 开发实例(2) 编译libwebsocket,封装jni函数,搭建IM通信基础服务
  11. java rgb8888转rgb565_Swift RGB888转RGB565
  12. gmx_MMPBSA--计算蛋白-配体自由能及能量分解
  13. 关于lock_guard
  14. 基于标签的蛋白质定量技术-iTRAQ,TMT
  15. blender操作说明
  16. 文件缓冲区和inode详解
  17. Unity录屏插件Recorder
  18. Libgdx slg游戏进程记录
  19. autojs教程:找图函数
  20. 刘润:思考维度越多,理解商业越深

热门文章

  1. 润乾V5与Java项目集成开发(1)
  2. 跨平台SCADA系统(组态软件)开发4
  3. dropbox下载及安装、使用
  4. QQ技术攻略-隐藏颇多秘密
  5. oracle创建用户分配权限
  6. python使用作为转义字符_当需要在字符串中使用特殊字符时,Python使用作为转义字符的起始符号...
  7. 漏掉的账目(用C语言去重)
  8. 现在还有人买Android平板吗,换代也没用我为什么不买安卓平板
  9. 阿里的程序猿要逆天 测试代码的健身单车了解一下!
  10. kaggle之电影文本情感分类