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. [转]清除服务器IIS缓存的常用方法

    转自:http://www.rrzzw.com/jishu/IIS/39.html 转自:http://cqujsjcyj.iteye.com/blog/736624 使用IIS来开虚拟主机空间架设网 ...

  2. date的讲解及分析

    Date() 对象(获取时间的)   不过是用类的写法来实现的:(他也没有私有属性,都是通过__proto__继承来的) Date()  分两大系      一个是get系列(及获取时间)     一 ...

  3. [转载] ./configure,make,make install的作用

    1.configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码:./configure –prefix=/u ...

  4. Game of War - Fire Age 有何特别之处?

    作者:福克斯007 链接:https://www.zhihu.com/question/21611550/answer/52458767来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  5. javascript的密封对象之seal(),isSealed()方法

    EcmaScrip5t中出现了密封对象概念.密封对象不可扩展,而已有的成员的[Configurable]特性被设置为false.也就是说属性和方法是不能删除的.但是是可以修改的. 示例一: var p ...

  6. python中函数的参数

    函数参数(一) 思考一个问题,如下: 现在需要定义一个函数,这个函数能够完成2个数的加法运算,并且把结果打印出来,该怎样设计?下面的代码可以吗?有什么缺陷吗? def add2num(): a = 1 ...

  7. pythone--002

    元组就是不可修改: 字典的索引不是自增的.  元组和列表是: 默认 是key 通过get  没有这个key  是none get可以有默认值: 通过索引 没有就报错. 检查字典中某个可以是否存在:ha ...

  8. 1.HTML编码解码URL替换--代码整理

    public class HtmlCode { public static String encode(String str){ String s = ""; if (str.le ...

  9. 在spring中实现quartz的动态调度(开始、暂停、停止等)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fantasic_van/article/details/74942062 需求: 需要在页面设定某个 ...

  10. 机器学习入门-使用GridSearch进行网格参数搜索GridSeach(RandomRegressor(), param_grid, cv=3)

    1.GridSeach(RandomRegressor(), param_grid, cv=3) GridSearch第一个参数是算法本身, 第二个参数是传入的参数组合, cv表示的是交叉验证的次数 ...