HDU - 3410 Passing the Message 单调递减栈
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.
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 与LC的课后辅导类似,这道用到了单调递减栈,分别找左右两端点,c记录最后一个出栈元素,递增栈退栈顶变小,递减栈退栈顶变大,第一个恰好比当前值大的前一个即为信使。
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
int t,tt,n,c,f,i;
int a[],l[],r[];
stack<int> s;
scanf("%d",&t);
tt=t;
while(t--){
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&a[i]);
}
c=;
memset(l,,sizeof(l));
memset(r,,sizeof(r));
for(i=;i<=n;i++){
f=;
while(s.size()&&a[s.top()]<=a[i]){
f=;
c=s.top();
s.pop();
}
l[i]=f==?:c;
s.push(i);
}
while(s.size()){
s.pop();
}
c=n;
for(i=n;i>=;i--){
f=;
while(s.size()&&a[s.top()]<=a[i]){
f=;
c=s.top();
s.pop();
}
r[i]=f==?:c;
s.push(i);
}
while(s.size()){
s.pop();
}
printf("Case %d:\n",tt-t);
for(i=;i<=n;i++){
printf("%d %d\n",l[i],r[i]);
}
}
return ;
}
HDU - 3410 Passing the Message 单调递减栈的更多相关文章
- hdu 3410 Passing the Message(单调队列)
题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...
- HDU 3410 Passing the Message
可以先处理出每个a[i]最左和最右能到达的位置,L[i],和R[i].然后就只要询问区间[ L[i],i-1 ]和区间[ i+1,R[i] ]最大值位置即可. #include<cstdio&g ...
- Passing the Message 单调栈两次
What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...
- hdu 3410 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 3410【单调栈】
思路: 单调栈. 鄙人的记忆:按当前为最大值的两边延伸就是维护单调递减栈. //#include <bits/stdc++.h> #include <iostream> #in ...
- Passing the Message
Passing the Message http://acm.hdu.edu.cn/showproblem.php?pid=3410 Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 5818 Joint Stacks(联合栈)
HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 3410 && POJ 3776 Passing the Message 单调队列
题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...
- hdu 4300 Clairewd’s message KMP应用
Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...
随机推荐
- kubernetes之StatefulSet详解
系列目录 概述 RC.Deployment.DaemonSet都是面向无状态的服务,它们所管理的Pod的IP.名字,启停顺序等都是随机的,而StatefulSet是什么?顾名思义,有状态的集合,管理所 ...
- kubernetes集群管理常用命令一
系列目录 我们把集群管理命令分为两个部分,第一部分介绍一些简单的,但是可能是非常常用的命令以及一些平时可能没有碰到的技巧.第二部分将综合前面介绍的工具通过示例来讲解一些更为复杂的命令. 列出集群中所有 ...
- kubernetes之故障现场二,节点名称冲突
系列目录 问题描述:测试环境由于异常断电导致服务器重启一后,有一个节点的状态一直是NotReady.通过journalctl -f -u kubelet没有错误日志输出.通过tail /var/log ...
- 深度解析开发项目之 03 - enum的使用
深度解析开发项目之 03 - enum的使用 01 - 在#import和@interface之间定义typedef enum 注意: 默认是0,1,2,3 02 - 定义可以操作的数据类型的属性 0 ...
- 使用sed来自动注释/恢复crontab中的一个任务
# 注释crontab任务crontab -l > ${WORK_DIR}/cron_binarysed -i 's%\(.*/home/xyz/xyz.sh\)%#\1%' ${WORK ...
- 实现单击列表头对ListView的动态排序
排序是根据列的类型来的,就ID列来说,int类型的排序结果是3,5,17,而如果你把该列类型改为string,结果就会是17,3,5,如果你定义列的时候不加类型,默认是string,如果是自定义类型, ...
- 用Cocoapods集成XMPPFramework 遇 Module 'KissXML' not found 问题
用Coacopods集成XMPPFramework完成后Command + B,报Module 'KissXML' not found 一般来说,通过Coacopods集成集成第三方框架,不会再有依赖 ...
- Windows消息、绘图与多线程
有一个项目,一旦点下按钮后,用死循环不停的读数据,读出后立刻用可视化的方法显示.如果不采用多线程的方法,程序运行都正确,但无法关闭窗口,不清楚是窗口无法通过关闭按钮来接受Windows消息,还是接受了 ...
- [自动化平台系列] - 初次使用 Macaca-前端自动化测试(1)
1. 所先看一下官方地址,了解一下这个是不是你想要的测试工具 https://macacajs.github.io/macaca/environment-setup.html 2. 去掉sudo -- ...
- holiday和vacation的区别
holiday:假日vacation:假期a.对于英国人或者澳大利亚人来说,“假日”的意思等同于“假期”(尽管他们很少用“假期”)b.如果你是美国人,“假日”是指一个特殊的日子,好像圣诞节,而“假期” ...