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])( ...
随机推荐
- 前端基础 & 初识CSS
CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素.l 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 每个CS ...
- MYSQL中case when then else end 用法
条件语句 delimiter \\CREATE PROCEDURE proc_if ()BEGIN declare i int default 0; if i = 1 THEN ...
- Codeforces 240F. TorCoder 线段树
线段树统计和维护某一区间内的字母个数.. . . F. TorCoder time limit per test 3 seconds memory limit per test 256 megabyt ...
- Linux学习笔记(1)linux的开关机及重启
linux的启动流程 一.启动 (1)电源 开关 (2)选择启动方式:FLOPPY/BIOS/CDROM(软盘/bios启动/光盘) 基于MBR引导方式 [1]MBR:最多只能划分4个主分区,逻辑 ...
- web.xml文件配置说明
web.xml作用: web.xml主要用来配置Filter.Listener.Servlet等,当我们去启动一个WEB项目时,容器(jetty.tomcat等)首先会读取项目web.xml配置文件里 ...
- ALE和IDocs
转自:http://blog.163.com/shenshengqge@126/blog/static/820512902011101152518635/ 作为目前ERP市场上最为领先的应用系统之一, ...
- 1.3 使用电脑测试MC20的电话语音功能
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- SQLServer导入Excel,复杂操作
导入Excel 先导入的时候报错了, 提示未在本地计算机上注册"Microsoft.ACE.Oledb.12.0"提供程序.(System.Data),去网址下个软件安装就搞定了, ...
- Oracle处理Clob类型数据入库(String入库)
从网上查找一堆参考,要么语焉不详,要么不可行.自己鼓捣了一堆可以正常入库了.请看最后: insert into CP_V_INFO" + "(ID, "+ "P ...
- ionic资源网站
http://ionichina.com/topic/570b1f4ecd63e4247a7cfcf3 http://doc.ionicmaterialdesign.com/#intro http:/ ...