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 python 序列
内置序列 容器类型 list .tuple和collections.deque这些序列能放入不同的类型的数据 扁平序列 str.byets.bytearray.memoryview(内存视图)和arr ...
- python实现复制整个目录的方法
import shutil #复制文件 shutil.copyfile('listfile.py', 'd:/test.py') #复制目录 shutil.copytree('d:/temp', 'c ...
- python并发之IO模型(二)
blocking IO (阻塞IO) 在linux中,默认情况下所有的socket都是blocking,一个典型的读操作流程大概是这样: 当用户进程调用了recvfrom这个系统调用,kernel就开 ...
- Java语言实现简单FTP软件------>FTP软件主界面的实现(四)
首先看一下该软件的整体代码框架 1.首先介绍程序的主入口FTPMain.java,采用了一个漂亮的外观风格 package com.oyp.ftp; im ...
- Python __setitem__()、__getitem__()、__delitem__()
转载:http://blog.csdn.net/xhw88398569/article/details/48690163 __xxxitem__:使用 [''] 的方式操作属性时被调用 __setit ...
- android studio DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs
DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs 参考https://stackoverflow.com/questions/38892 ...
- win10笔记本触摸板手势大全
- Java基础—抽象类和接口
1.抽象类 在Java语言中使用abstrac关键字来定义抽象类和抽象方法,抽象方法没有定义,方法名后面直接跟一个分号,而不是花括号. public abstract class Employee { ...
- 爬虫基础库之requests模块
一.requests模块简介 使用requests可以模拟浏览器请求,比起之前用到的urllib,requests模块的api更加快捷,其实ruquests的本质就是封装urllib3这个模块. re ...
- java中byte数组与int,long,short间的转换
http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...