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大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...
随机推荐
- Servlet之javax.servlet包
链接 : http://blog.sina.com.cn/s/blog_5d4214c70102wnf1.html
- hihoCoder 1636 Pangu and Stones
hihoCoder 1636 Pangu and Stones 思路:区间dp. 状态:dp[i][j][k]表示i到j区间合并成k堆石子所需的最小花费. 初始状态:dp[i][j][j-i+1]=0 ...
- .net开发中,C# DateTime.Now 取出的时间含有星期解决办法
1.开始→运行→输入regedit,打开注册表编辑器,找到HKEY_USERS. 2.ctrl+f 查找sShortDate(多查询几次,将所有的都查出来). 3.在右边的窗口中找到sDate项,将其 ...
- 为arm 编译包含gd的php5
1) 下载gd的各种依赖包. 但是不要下载gd本身,因为这是包含在php里的. 探索的时候也下载了 libvpx freetype,可惜最后的编译没过,就没有用上 2)编译各种(编译前记得把各种环境变 ...
- Asp.Net MVC中Action跳转
首先action的跳转大致归类: 1跳转到与当前同一控制器内的action和不同控制器内的action. 2带有参数的action跳转和不带参数的action跳转. 3跳转到指定视图,不经过Contr ...
- Jaccard similarity(杰卡德相似度)和Abundance correlation(丰度相关性)
杰卡德距离(Jaccard Distance) 是用来衡量两个集合差异性的一种指标,它是杰卡德相似系数的补集,被定义为1减去Jaccard相似系数.而杰卡德相似系数(Jaccard similarit ...
- 修改TOMCAT默认主页
1.默认的项目都在目录:apache-tomcat-9.0.0.M22\webapps\ROOT下 2.该目录下有一个index.jsp是tomcat的默认主页,如下 3.现在需要修改这个默认主页,改 ...
- layui的富文本编辑器怎么赋值
除了上面讲的方法外,还可以使用layedit自带的方法赋值/*** 设置编辑器内容* @param {[type]} index 编辑器索引* @param {[type]} content 要设置的 ...
- Leetcode 77
//这似乎是排列组合的标准写法了已经class Solution { public: vector<vector<int>> combine(int n, int k) { v ...
- C/C++中 malloc和new区别
1. malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.它们都可用于申请动态内存和释放内存. new 是个操作符,和什么"+"," ...