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.
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 <cstdio>
using namespace std; int stack[],l[],r[],top,a[],t,n,flag; int main()
{
scanf("%d",&t);
for(int k=;k<=t;k++)
{
top=;
scanf("%d",&n);
a[]=a[n+]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++)
{
flag=;
while(top&&a[i]>=a[stack[top-]])
{
flag=;
top--;
}
if(flag)l[i]=stack[top];
else l[i]=;
stack[top++]=i;
}
top=;///第二次从右往左用单调栈 一定要更新
for(int i=n;i>=;i--)
{
flag=;
while(top&&a[i]>=a[stack[top-]])
{
flag=;
top--;
}
if(flag)r[i]=stack[top];
else r[i]=;
stack[top++]=i;
}
printf("Case %d:\n",k);
for(int i=;i<=n;i++)
printf("%d %d\n",l[i],r[i]);
}
}
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 “Sun Flo ...
- hdu 3410 Passing the Message(单调队列)
题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...
- HDU 3410 && POJ 3776 Passing the Message 单调队列
题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...
- hdu3410-Passing the Message(RMQ,感觉我写的有点多此一举。。。其实可以用单调栈)
What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...
- HUID 5558 Alice's Classified Message 后缀数组+单调栈+二分
http://acm.hdu.edu.cn/showproblem.php?pid=5558 对于每个后缀suffix(i),想要在前面i - 1个suffix中找到一个pos,使得LCP最大.这样做 ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]
4453: cys就是要拿英魂! Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 90 Solved: 46[Submit][Status][Discu ...
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- BZOJ1057[ZJOI2007]棋盘制作 [单调栈]
题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...
随机推荐
- python下编译py成pyc和pyo和pyd
https://www.cnblogs.com/dkblog/archive/2009/04/16/1980757.html
- LeetCode--122--卖卖股票的最佳时机II
问题描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易( ...
- 用Rails.5.2+ Vue.js做 vue-todolist app
Rails5.2+Vue.js完成Lists(curd) 注意: Edit/update使用SPA(single-page Application单页面程序)的方法完成.点击文字出现一个输入框和按钮. ...
- codeforces 576c// Points on Plane// Codeforces Round #319(Div. 1)
题意:有n个点,找到一个顺序走遍这n个点,并且曼哈顿距离不超过25e8. 由于给的点坐标都在0-1e6之间.将x轴分成1000*1000,即1000长度为1块.将落在同一块的按y排序,编号为奇的块和偶 ...
- 求逆序数的方法--线段树法&归并排序法
逆序数的概念:对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,就说有1个逆 ...
- Confluence 6 使用 LDAP 授权连接一个内部目录 - 拷贝用户到登录
在登录时拷贝用户(Copy User on Login) 这个选项在用户尝试登录的时候将会被触发.如果这个选择框被选择的话,当用户使用 LDAP 授权的用户名和密码登录系统的时候,用户将会在内部目录自 ...
- Type cvc-complex-type.2.4.a: Invalid content was found starting with element 'build'.错误的解决方法
项目突然间爆出了这样的问题: Description Resource Path Location Typecvc-complex-type.2.4.a: Invalid content was fo ...
- 2-24-源码编译搭建LAMP环境-作业 ( By 小甘丶 )
安装上课的内容要求: 实验环境: VMware Virtual Machine : System Version: CentOS6.8 ( Gan35 ) IP Address : 192.168.3 ...
- mysql语句查询时间检测
explain的使用 1.首先我们是要登入你的mysql的,然后选择数据库输入:use 你要选择的库名 2执行语句 eg: explain SELECT * FROM wish_orders1412 ...
- 解析XML文档大致流程以及相关方法
---恢复内容开始--- 使用dom解析XML文档的大致流程(要导入dom4j)1:创建SAXReader2:使用SAXReader读取xml文档,并生成对应的Document对象,该对象保存了该文档 ...