Passing the Message

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.

Input

The 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 .

Output

For 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

题意:对每个a[i]求出左右两边,比自己小的且最大的元素

思路:左边,建立从左至右的单调递减栈:

a[i]>top,出栈并每次记录top,直至a[i]>top,此时上一个top就是比a[i]小的左边最大元素;

a[i]<top,由于栈单调递减,栈顶元素是最小的所以,此时无符合的元素。

空栈,不存在符合的元素。

右边同理。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn=50005;int a[maxn];
int l[maxn];
int r[maxn];
int main()
{int t;int n;scanf("%d",&t);int ans=1;while(t--){scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);stack<int> st;memset(l,0,sizeof l);memset(r,0,sizeof r);for(int i=1;i<=n;i++)//找到左边最高的,从左往右单调递减栈{if(st.empty()){l[i]=0;st.push(i);}else if(a[i]>a[st.top()]){while(!st.empty()&&a[i]>a[st.top()]){l[i]=st.top(); //top将会被弹出栈,top满足比a[i]小且在左边最大st.pop();}st.push(i);}else if(a[i]<a[st.top()]){l[i]=0;//左边没有比他小的st.push(i);}}stack<int> p;for(int i=n;i>=1;--i)//找右边的比自己小的最大值,从右往左建立递减栈{if(p.empty()) r[i]=0;else if(a[i]>a[p.top()]){while(!p.empty()&&a[i]>a[p.top()]){r[i]=p.top();p.pop();}}else if(a[i]<a[p.top()]){r[i]=0;}p.push(i);}cout << "Case " <<  ans++<<  ":" << endl;for(int i = 1;i <= n;i++)cout << l[i] << " " << r[i] << endl;}
}

单调栈 左右传消息 HDU 3410相关推荐

  1. 数据结构 单调栈+几何 摩天大楼【HDU 5033 】

    HDU 5033 题目大意: 就是一个人来到充满摩天大楼的城市,所有大楼没有宽度.建一直角坐标系,给出每个建筑的高度,现在求人站在(x,0)处能够看到天空的范围.(即不被摩天大楼阻挡).答案只需要给出 ...

  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. HDU多校4 - 6989 Didn‘t I Say to Make My Abilities Average in the Next Life?!(单调栈)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,再给出 mmm 次询问,每次询问给出一个区间 [l,r][l,r][l,r],要求输出区间 [l,r][l,r][l,r] 内 " ...

  4. HDU 6194 后缀数组+单调栈

    题意: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6194 找到一个字符串中恰好出现k次的子串的数目. 思路: 计算出height数组,根据heig ...

  5. HDU 1506 解题报告 Largest Rectangle in a Histogram (单调栈)

    看题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题意比较明显,就是找以某一个矩形为高的最大的矩形.这个题可以用单调栈来求解,需要注意的是如果从 ...

  6. HDU 6052 To my boyfriend(容斥+单调栈)

    题意:对于一个n*m的方格,每个格子中都包含一种颜色,求出任意一个矩形包含不同颜色的期望. 思路: 啊啊啊啊啊,补了两天,总算A了这道题了,简直石乐志,前面的容斥还比较好写,后面的那个>13那个 ...

  7. hdu 1506(dp || 单调栈)

    题意:这题是要找最大的矩形面积. 解题思路:这题的关键是要找每个条形能够往左和往右能够到达的最大长度.我最开始的思路是单调栈去维护,只要入栈的元素比栈顶元素小,栈顶就要出栈,并且知道其最右能够到达的最 ...

  8. hdu 4252(单调栈)

    题意:一张相片上的很多建筑相互遮住了,根据高低不同就在相片上把一座高楼的可见部分作为一个矩形,并用数字描述其高度,若一张相片上的两个建筑群中间有空地,高度则为0;求最少有多少个建筑; 解题思路:这道题 ...

  9. HDU 1506 Largest Rectangle in a Histogram(dp、单调栈)

    你是不是飘了?骚年! Problem Description A histogram is a polygon composed of a sequence of rectangles aligned ...

最新文章

  1. matPlotLib绘制决策树
  2. leetcode 718. Maximum Length of Repeated Subarray | 718. 最长重复子数组(动态规划)
  3. Jekyll 使用入门
  4. 多线程—线程池Executor框架及四种常用线程池
  5. 百般受虐!“波士屯动力”机器人这一次枪口对准人类
  6. 重磅!新增一个假期!此地已正式发文!
  7. 通勤一小时,堵车半小时,AI 救救社畜
  8. data-mask遮罩无法正常显示与编辑的问题
  9. 2复数与复变函数(二)
  10. Android——适配器其他组件(AutoCompleteTextView:自动完成文本编辑框;Spinner:下拉列表)...
  11. C#正则表达式提取txt小说目录
  12. 计算机怎么在表格里打勾,怎样在Excel输入对号√,Excel单元格怎么输入对号(方框内打勾)?...
  13. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte解决办法
  14. 关于kali中base64的加解密使用
  15. Pixhawk指示灯的含义
  16. JetBrains的注册
  17. 西门子PLC1200学习之数学运算功能的应用
  18. IDE for C++
  19. 时速云发布微服务产品 TMF V5.6.0 全新版本,支持独立部署,简化企业微服务架构
  20. 高德地图路径轨迹起点标点不变_前有百度高德,后有华为滴滴,腾讯地图该如何上演王者归来的戏码?...

热门文章

  1. 数学分析教程史济怀练习15.4
  2. 车载液晶驱动板(RTD2660H) Fondar USB下载/烧录器 驱动安装
  3. 微服务openid等_使用OpenID Connect在Quarkus中保护微服务
  4. 计算机网络(非常全,建议收藏)
  5. FPS游戏服务器设计的问题
  6. Openssh 升级
  7. 微信小程序—swiper轮播图图片不显示的解决方法
  8. “adb”不是内部或外部命令——解决方案
  9. 葛底斯堡演说 只出现一次单词
  10. VMware vSphere Web Services SDK编程指南(二)-Java 开发设置