Passing the Message(单调栈)
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(单调栈)相关推荐
- HDU - 3410 Passing the Message 单调递减栈
Passing the Message What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Su ...
- 【HDU - 3410 】 Passing the Message(单调栈)
题干: What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Sun Flower" k ...
- HDU3410 Passing the Message 【单调栈】
题意 一个小孩可以看到左边比他矮,但是最高的孩子,同样可以看到右边比他矮,但是最高的小孩 分析 求左边比他矮,但是最高的孩子:维护一个单调栈,从栈顶到栈底单调递增: 从左边第一个孩子开始,如果栈为空或 ...
- 线性结构 —— 单调栈与单调队列
[单调栈] 1.原理 单调栈,就是栈内元素保持一定单调性(单调递增或单调递减)的栈,即从栈底到栈顶单调递增或递减. 对于单调递增的栈,如果栈为空或入栈元素值大于等于栈顶元素值,则入栈:否则,若入栈会破 ...
- 解题报告 (十) 单调栈
文章目录 单调栈 解题报告 PKU 2082 Terrible Sets HDU 2430 Beans HDU 4252 A Famous City PKU 2796 Feel Good HDU 34 ...
- 单调栈 左右传消息 HDU 3410
Passing the Message What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Su ...
- POJ2796 Feel Good(单调栈)
题意: 给出一列数据,要求一个区间内最小值与区间内数据总和乘积最大值 要点: 还是单调栈,这次我自己写的,先做了几题比较简单的果然还是有效果的,这题也是一样,按点遍历,网上大神做的是直接遍历一次即可, ...
- 【单调栈 前缀和 异或】7.21序列求和
还要再细细思考的奇妙思路 题目描述 小A最近喜欢上了关于区间max的问题.她定义一个区间的价值是max(ai)(l<=i<=r)∗(alxoral+1xor...xorar)max(ai) ...
- 栈与队列7——单调栈结构(进阶问题)
题目 一个含有重复值的数组arr,找到每一个i位置左边和右边离i位置最近且值比arr[i]小的位置,返回所有相应的信息. 举例:arr={3,4,1,5,6,2,7},返回如下的二维数组作为结果:{{ ...
最新文章
- 露雨资源库(第一个.net2.0软件)二
- oracle导入导出命令
- 皖南医学院2020C语言试卷,安徽继续教育在线 - 皖南医学院
- java保持运行_保持Java程序无限运行的方法是有效的吗?
- Java中局部变量必须初始化
- iOS CGRectGetMaxX/Y 使用
- 信息学奥赛一本通 1312:【例3.4】昆虫繁殖
- java读取本地图片的绝对地址_我想使用图片的绝对路径。为什么java不能使用图片的绝对路径。...
- RouterOS 5.22固定公网IP共享上网设置
- [JNI] 开发实例(2) 编译libwebsocket,封装jni函数,搭建IM通信基础服务
- java rgb8888转rgb565_Swift RGB888转RGB565
- gmx_MMPBSA--计算蛋白-配体自由能及能量分解
- 关于lock_guard
- 基于标签的蛋白质定量技术-iTRAQ,TMT
- blender操作说明
- 文件缓冲区和inode详解
- Unity录屏插件Recorder
- Libgdx slg游戏进程记录
- autojs教程:找图函数
- 刘润:思考维度越多,理解商业越深