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

Problem Description
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.

 
Input
The 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 .
 
Output
For 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
 
Source
 

栈内顺序递减

 #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的更多相关文章

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

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

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

  3. Passing the Message 单调栈两次

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

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

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

  5. HDU 3410 Passing the Message

    可以先处理出每个a[i]最左和最右能到达的位置,L[i],和R[i].然后就只要询问区间[ L[i],i-1 ]和区间[ i+1,R[i] ]最大值位置即可. #include<cstdio&g ...

  6. windows消息机制详解(转载)

    消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序.消息本身是作为一个记录传递给应用程序的 ...

  7. hdu3410 单调队列

    Passing the Message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. WM (Constants)

    Create page WM (Constants)   Summary WM_* Constants and their definitions or descriptions and what c ...

  9. VCL -- Understanding the Message-Handling System

    Understanding the Message-Handling System http://docwiki.embarcadero.com/RADStudio/XE7/en/Understand ...

随机推荐

  1. 开发框架-APP:Hybird App

    ylbtech-开发框架-APP:Hybird App Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App良好用户交互体 ...

  2. [转] AForge.NET 图像处理类

    https://www.nuget.org/packages?q=AForge.NET https://baike.baidu.com/item/AForge.NET/114415?fr=aladdi ...

  3. 如何将JQUERY对象转成Javascript对象

    问: <div id="test"></div> $("#test") //由Javascript对象转为Jquery对象: 但是如何转 ...

  4. Java5,Java 6,Java 7,Java 8新特性

    Java5: 1.泛型 Generics:        引用泛型之后,允许指定集合里元素的类型,免去了强制类型转换,并且能在编译时刻进行类型检查的好处. Parameterized Type作为参数 ...

  5. mysql响应时间超时排查

    背景: 数据库运营环境,zabbix mysql响应时间告警,响应时间超时 zabbix监控 tcprstart 直接抓包响应时间看到每5秒钟就一次,与zabbix监控一致 [root@slave1( ...

  6. php while循环控制实例讲解

    while循环是PHP中最简单的循环,其基本格式为: while (expr){ statement } 或者 while (expr): statement endwhile; 该语法表示,只要ex ...

  7. 洛谷P1415 拆分数列

    题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输出使得最后一个数最小的同时 ...

  8. CUDA C Programming Guide 在线教程学习笔记 Part 11

    ▶ 数学函数 ● 舍入函数,考虑被舍入参数有双精度浮点和单精度浮点,舍入方式有区别,舍入结果有整形.长整形和长长整形,所以共有以下舍入函数. // math_functions.h extern __ ...

  9. 为挂载到/home的RAID磁盘组扩容

    公司一台DELL服务器,安装的Ubuntu16.04系统,原来是6块1.2T的SAS盘做RAID-5挂载到/home,现在/home空间不够用了,需要扩容,再增加2块1.2T的盘.整个操作不复杂,但有 ...

  10. c# DbProviderFactories 多数据库支持工程模式

    DbProviderFactories.GetFactory(dbProviderName) DBProviderFactory factory = DBProviderFactorys.GetFac ...