Passing the Message
Passing the Message
http://acm.hdu.edu.cn/showproblem.php?pid=3410
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1051 Accepted Submission(s): 683
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 .
栈内顺序递减
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<stack>
#include<queue>
#include<cstdio>
#define ll long long
const long long MOD=;
#define maxn 50005
using namespace std; struct sair{
int v,pos,L,R;
} a[maxn]; int main(){
std::ios::sync_with_stdio(false);
int t;
cin>>t;
int co=;
while(t--){
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i].v;
a[i].pos=i;
a[i].L=a[i].R=;
}
stack<sair>st;
int pos;
for(int i=;i<=n;i++){
if(st.empty()){
st.push(a[i]);
}
else{
pos=;
while(st.size()&&st.top().v<=a[i].v){
a[st.top().pos].R=pos;
pos=st.top().pos;
st.pop();
}
if(pos!=-){
a[i].L=pos;
}
st.push(a[i]);
}
}
pos=st.top().pos;
st.pop();
sair tmp;
while(st.size()){
tmp=st.top();
a[tmp.pos].R=pos;
pos=tmp.pos;
st.pop();
}
cout<<"Case "<<++co<<":"<<endl;
for(int i=;i<=n;i++){
cout<<a[i].L<<" "<<a[i].R<<endl;
}
} }
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 单调递减栈
Passing the Message What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flo ...
- Passing the Message 单调栈两次
What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...
- HDU 3410 && POJ 3776 Passing the Message 单调队列
题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...
- HDU 3410 Passing the Message
可以先处理出每个a[i]最左和最右能到达的位置,L[i],和R[i].然后就只要询问区间[ L[i],i-1 ]和区间[ i+1,R[i] ]最大值位置即可. #include<cstdio&g ...
- windows消息机制详解(转载)
消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序.消息本身是作为一个记录传递给应用程序的 ...
- hdu3410 单调队列
Passing the Message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- WM (Constants)
Create page WM (Constants) Summary WM_* Constants and their definitions or descriptions and what c ...
- VCL -- Understanding the Message-Handling System
Understanding the Message-Handling System http://docwiki.embarcadero.com/RADStudio/XE7/en/Understand ...
随机推荐
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- 关于文章cisco漏洞4786
查看确认方法 [root@nodchen ~]# nmap -p4786 172.30.4.2 Starting Nmap 5.51 ( http://nmap.org ) at 2017-05-20 ...
- mysql主从复制——双机互为主从
第一.mysql主从复制(一主一从)怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下:首先需要做一些清理工作,如果之前配置了主从,但是配置失败了.结果会在/var/lib/mysql/ ...
- xiao look 知识贴
从事中医临床近二十年了,多少总是积累了点经验,本来准备将来老了经验更丰富的时候传给子女的,可惜儿子根本不打算学医.在这个论坛里也混了不短了,感觉这里的风气很纯正,也有不少立志于中医的人士.为此,我决定 ...
- nginx配置详解(转)
Nginx 配置文件详解 user nginx ; #用户 worker_processes 8; #工作进程,根据硬件调整,大于等于cpu核数 error_log logs/nginx_error. ...
- 在visual code的debugger for chrome中调试webpack构建的项目
一直使用chrome中内置的调试器, 感觉世界那么美好, 自从学了react之后,使用visual code作为编辑器, 它提供了很多插件, 其中就包括debugger for chrome, 一款使 ...
- linux移动复制删除命令
用mv命令1.作用mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中.该命令等同于DOS系统下的ren和move命令的组合.它的使用权限是所有用户.2.格式mv [options] 源文件或 ...
- 数据库之SQL笛卡尔积
1.笛卡尔积定义 笛卡尔积在SQL中的实现方式既是交叉连接(Cross Join).所有连接方式都会先生成临时笛卡尔积表,笛卡尔积是关系代数里的一个概念,表示两个表中的每一行数据任意组合,上图中两个表 ...
- 使用html和CSS进行网页网站设计 -- 简明步骤
网页制作流程: 1. 心中有规划,网站的骨架结构,页面布局layout. 2. 创建一个用于创建模板dwt的html页: main.html 3. 制作main.html: (1) 在html文件中依 ...
- 3. 修改myeclipse工作区间默认编码和jsp的默认编码
1.windows - preferences - General - Workspace 2.windows - preferences - MyEclipse - Files and Editor ...