hdu 3410 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=3410
Passing the Message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 827 Accepted Submission(s): 546
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.
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 .
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)
5
5 2 4 3 1
5
2 1 4 3 5
0 3
0 0
2 4
0 5
0 0
Case 2:
0 2
0 0
1 4
0 0
3 0
#include <iostream>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAX = ;
int a[MAX], l[MAX], r[MAX];
int main()
{
int N, i, j, k, t;
scanf("%d", &t);
for (int cas = ;cas <= t;++cas)
{
stack<int>S;
//memset(l, 0, sizeof(l));
//memset(r, 0, sizeof(r));
scanf("%d", &N);
for (i = ;i <= N;++i) scanf("%d", &a[i]);
for (i = ;i <= N;++i) {
if (S.empty() || a[i]<a[S.top()]) {
l[i] = ;
S.push(i);
}
else {
while (!S.empty()&&a[S.top()]<a[i]) {
l[i] = S.top();
S.pop();
}
S.push(i);
}
}while (!S.empty())S.pop();
for (i = N;i >= ;--i) {
if (S.empty() || a[i]<a[S.top()]) {
r[i] = ;
S.push(i);
}
else {
while (!S.empty() && a[S.top()]<a[i]) {
r[i] = S.top();
S.pop();
}
S.push(i);
}
}
printf("Case %d:\n", cas);
for (i = ;i <= N;++i) printf("%d %d\n", l[i], r[i]);
}
return ;
}
hdu 3410 单调栈的更多相关文章
- hdu 1506 单调栈问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空 ...
- hdu 5033 单调栈 ****
看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...
- hdu 5875(单调栈)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5033 (单调栈维护凸包) Building
题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑 ...
- hdu 4923 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...
- hdu 1505 单调栈升级版
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...
- hdu 1506 单调栈
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...
- HDU 3410【单调栈】
思路: 单调栈. 鄙人的记忆:按当前为最大值的两边延伸就是维护单调递减栈. //#include <bits/stdc++.h> #include <iostream> #in ...
- hdu 3410 Passing the Message(单调队列)
题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...
随机推荐
- Python——用正则求时间差
如有求时间差的需求,可直接套用此方法: import time true_time=time.mktime(time.strptime('2017-09-11 08:30:00','%Y-%m-%d ...
- 协程 Gevent
# 协程应用:爬虫 from gevent import monkey;monkey.patch_all() import gevent import requests import time def ...
- JS单例
s = (function S(){ var bean; function get(){ if(bean){ return bean }else{ bean = T(); return bean; } ...
- F110 参数保存和重新运行录屏
**初始界面回车 PERFORM frm_dynpro USING ' 'X'. PERFORM frm_dynpro USING '' 'BDC_CURSOR' 'F110V-LAUFD'. PER ...
- Nginx和php-fpm部署到不同的服务器
Nginx安装滤过,基本上nginx上的配置很少,只要添加个server就可以了,主要安装php-fpm服务 php7.1.3安装 1.安装依赖的软件包yum -y install gcc gcc-c ...
- List Slice in Python(Compared with Java)
Python: 在Python中, 对于list, 切片会返回一个新的list, 而不会改变原有的list. 注意这儿说的"不会改变原有的list"指的是下面的这种情况: a = ...
- BigDecimal类型、Long类型判断值是否相等,以及BigDecimal加减乘除
//Long是需要比较精度的,所以要用longValueif(project.getFriendId().longValue() != friendId.longValue()) { return t ...
- 【转】Python爬虫(1)_基本原理
一 爬虫是什么 #如果我们把互联网比作一张大的蜘蛛网,数据便是存放于蜘蛛网的各个节点,而爬虫就是一只小蜘蛛,沿着网络抓取自己的猎物/数据 #爬虫指的是:向网站发起请求,获取资源后分析并提取有用数据的程 ...
- POJ 3468 A Simple Problem with Integers 【线段树】
题目链接 http://poj.org/problem?id=3468 思路 线段树 区间更新 模板题 在赋初始值的时候,按点更新区间就可以 AC代码 #include <cstdio> ...
- jquery的autocomplete在firefox下不支持中文输入法的bug
Query.Autocomplete 是jquery的流行插件,能够很好的实现输入框的自动完成(autocomplete).建议提示(input suggest)功能,支持ajax数据加载. 但唯一遗 ...