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 <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 单调栈两次的更多相关文章

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

  2. hdu 3410 Passing the Message(单调队列)

    题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...

  3. HDU 3410 &amp;&amp; POJ 3776 Passing the Message 单调队列

    题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...

  4. hdu3410-Passing the Message(RMQ,感觉我写的有点多此一举。。。其实可以用单调栈)

    What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...

  5. HUID 5558 Alice's Classified Message 后缀数组+单调栈+二分

    http://acm.hdu.edu.cn/showproblem.php?pid=5558 对于每个后缀suffix(i),想要在前面i - 1个suffix中找到一个pos,使得LCP最大.这样做 ...

  6. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  7. BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]

    4453: cys就是要拿英魂! Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 46[Submit][Status][Discu ...

  8. BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2326  Solved: 1054[Submit][Status ...

  9. BZOJ1057[ZJOI2007]棋盘制作 [单调栈]

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...

随机推荐

  1. cookie session localstorage sessionStorage区别

    cookie:http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html 重要特点: 1.cookie 有大小设置,有过期时间设 ...

  2. C#快速生成数据数组

    需求:生成一个数组,数组里面的值为1-100实现方式:拿到这个需求很多朋友可能会想到一个快速实现的方式如下: ]; ;i<=;i++){ arr[i]=i; } 但是C#提供了一个快速生成的方式 ...

  3. C#使用xpath查找xml节点信息

    Xpath是功能很强大的,但是也是相对比较复杂的一门技术,最好还是到博客园上面去专门找一些专业的帖子来看一看,下面是一些简单的Xpath语法和一个实例,提供给你参考一下. xml示例: <?xm ...

  4. js中如何访问对象和数组

    js中如何访问对象和数组 一.总结 一句话总结:js访问对象点和中括号,访问数组的话就是中括号 对象 . [] 数组 [] 1.js访问对象的两种方式? . [] 可以使用下面两种方式访问对象的属性和 ...

  5. win7创建 VirtualBox COM 对象失败。 应用程序现在将终止。 Callee RC: E_NOINTERFACE (0x80004002)

    win7创建 VirtualBox COM 对象失败.  应用程序现在将终止.    Callee RC: E_NOINTERFACE (0x80004002) 启动VirtualBox提示这个错误, ...

  6. 雷林鹏分享:C# 特性(Attribute)

    C# 特性(Attribute) 特性(Attribute)是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签.您可以通过使用特性向程序添加声明性信息.一个声明 ...

  7. ORA-14452:试图创建,更改或删除正在使用的临时表中的索引

    因为表kol_xx_fin050_temp 为临时表,而且有其他session正在使用. select vs.* from v$session vs , v$lock vl , dba_objects ...

  8. Centos7 docker 常用指令

    Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3.10 以上 一.docker的安装及卸载 1.查看当前系统内核版本: [root@docker ~]# uname - ...

  9. install rabbitvcs in ubuntu16.04

    reference: https://github.com/rabbitvcs/rabbitvcs how to install : sudo apt-get install rabbitvcs-cl ...

  10. Python实现简单的爬虫获取某刀网的更新数据

    昨天晚上无聊时,想着练习一下Python所以写了一个小爬虫获取小刀娱乐网里的更新数据 #!/usr/bin/python # coding: utf-8 import urllib.request i ...