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大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...
随机推荐
- lambda表达式/对象引用计数
★lambda表达式的用法例:I=[(lambda x: x*2),(lambda y: y*3)]调用:for x in I: print x(2)输出:4,6 ★获取对象的引用次数sys.getr ...
- 移动端视频h5表现问题汇总
1. 同屏播放视频 <video src="" x-webkit-airplay="true" webkit-playsinline="true ...
- 百度echart如何动态生成图表
百度echart如何动态生成图表 一.总结 一句话总结: clear hideloading setOption 主要是下面三行代码: myChart.clear(); //清空画布myChart.h ...
- RabbitMq windows 安装
参考官方网址: http://www.rabbitmq.com/install-windows-manual.html http://www.rabbitmq.com/install-windows. ...
- hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)
描述 Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us h ...
- python 小练习 9
还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 .............. 先 ...
- Oracle HRMS APIs
Oracle HRMS APIs..... Here I will be sharing all the Oracle HRMS APIs related articles. 参考地址: Oracle ...
- mkfs.ext3 option
mkfs.ext3 OPTIONS -b block-size Specify the size of blocks in bytes. Valid block-size values are ...
- poj3020 二分图匹配 最大独立集
这是一道水题, 这里是最大流解法,之后再补 坑在又忘了反向建边了 题意:给你二维bool数组,让你求出能用多米诺骨牌覆盖所有 1 且骨牌最少的放法(因为多米诺骨牌1*2的结构方便描述,原题没有),原本 ...
- (C/C++学习笔记) 九. 变量的存储类型
九. 变量的存储类型 ● 变量的存储类型(见附页) ● 注释 ①对于自动变量,它属于动态存储方式. 但是也可以用static定义它为静态自动变量,或称静态局部变量,从而成为静态存储方式.由此看来,一个 ...