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])( ...
随机推荐
- php安装pcntl扩展
1.下载一个同样版本的php(我的是php5.6.27,我下的是php5.6.28) [root@CentOS6 lamp]# wget http://hk1.php.net/get/php-5.6. ...
- ThinkPHP的join方法
两张表: 表一:pre_company_member 关联字段:comp_id 表二:pre_company 关联字段:comp_id 查询这两表中的数据. 方法一:驼峰法 $member=M('C ...
- DevOps能力是落地微服务的前提
在软件开发领域不存在银弹,当用一项新的技术或新的架构时一定要明白其背后的原理,确保把合适的技术应用在合适的项目上,而不是盲目跟风. 单体应用伸缩性差,而且随着应用规模的扩大,业务逻辑和开发部署过程都变 ...
- 【LeetCode】【矩阵旋转】Rotate Image
描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- Python学习进程(8)字符串內建函数
Python字符串內建函数实现了string模块的大部分方法,并包括了对Unicode编码方式的支持. (1)capitalize(): 将字符串的第一个字母变成大写,其他字母变小写. ...
- 约瑟夫环的C语言数组实现
约瑟夫环问题的具体描述是:设有编号为1,2,……,n的n个(n>0)个人围成一个圈,从第1个人开始报数,报到m时停止报数,报m的人出圈,才从他的下一个人起重新报数,报到m时停止报数,报m的出圈, ...
- springboot获取URL请求参数的几种方法
原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于pos ...
- Vue全家桶 vue + vue-router + vuex
Vue实例的生命周期钩子函数(8个) 1. beforeCreate data属性光声明没有赋值的时候 2. created ...
- Pytorch的gather用法理解
先放一张表,可以看成是二维数组 行(列)索引 索引0 索引1 索引2 索引3 索引0 0 1 2 3 索引1 4 5 6 7 索引2 8 9 10 11 索引3 12 13 14 15 看一下下面例子 ...
- Go make 和 new的区别
在Go语言中: make 被用来分配引用类型的内存: map, slice, channel new 被用来分配除了引用类型的所有其他类型的内存: int, string, array等 本文主要给大 ...