HDU3410(单调队列)
Passing the Message
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 643    Accepted Submission(s): 413
Problem Description
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
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
Sample Input
Sample Output
//2016.8.24
#include<iostream>
#include<cstdio>
#include<deque> using namespace std;
const int N = ;
int l[N], r[N], a[N]; int main()
{
int T, n, kase = ;
cin>>T;
while(T--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
deque<int> q;
for(int i = ; i <= n; i++)
{
int fr = , ba = ;
while(!q.empty()&&a[q.back()]<a[i])
{
fr = ;
ba = q.back();
q.pop_back();
}
if(fr) l[i] = ;
else l[i] = ba;
q.push_back(i);
}
for(int i = n; i >= ; i--)
{
int fr = , ba = ;
while(!q.empty()&&a[q.back()]<a[i])
{
fr = ;
ba = q.back();
q.pop_back();
}
if(fr) r[i] = ;
else r[i] = ba;
q.push_back(i);
}
printf("Case %d:\n", ++kase);
for(int i = ; i <= n; i++)
printf("%d %d\n", l[i], r[i]);
}
return ;
}
HDU3410(单调队列)的更多相关文章
- hdu3410 单调队列
		
Passing the Message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
 - BestCoder Round #89  B题---Fxx and game(单调队列)
		
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945 问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路: B ...
 - 单调队列 && 斜率优化dp 专题
		
首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...
 - FZU 1914 单调队列
		
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
 - BZOJ 1047 二维单调队列
		
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...
 - 【BZOJ3314】  [Usaco2013 Nov]Crowded Cows 单调队列
		
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
 - BZOJ1047: [HAOI2007]理想的正方形 [单调队列]
		
1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2857 Solved: 1560[Submit][St ...
 - hdu 3401 单调队列优化DP
		
Trade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
 - 【转】单调队列优化DP
		
转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...
 - hdu3530 单调队列
		
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
 
随机推荐
- js 选项卡
			
<html><head lang="en"> <meta charset="UTF-8"> <title>Tab ...
 - [iOS]C语言技术视频-05-程序循环结构(do{}while();)
			
下载地址: 链接: http://pan.baidu.com/s/1c02ke3m 密码: x97w
 - ecos新命令
			
创建myapp,在myapp里创建lib/command目录 新建一个文件hello.php <?php /** * myapp_command_hello(myapp->app名称,co ...
 - NSMutableDictionary
			
NSDictionary *dic = @{@"name":@"yj", @"age":@"24", @"ho ...
 - hadoop重新启动之后Datanode无法启动的问题
			
每次将hadoop重新启动之后我们查看进程就会发现,namenode成功启动,然而datanode却不能重新启动,格式化以后也不行,百思不得其解,最后在终于在厦门大学的一篇博客里面找到了解决的方法,我 ...
 - 20、手把手教你Extjs5(二十)模块Grid的多列表方案
			
对于有很多字段的模块在一个grid中显示所有的字段,会显得很臃肿,对于不同的用户其侧重的字段类型也不尽相同,因此就有必要为Grid的列表设计多个方案.在这个自定义系统进行设计的时候,我已经将这部分内容 ...
 - LPC1768的USB-相关结构体定义
			
#ifndef __USB_H__ #define __USB_H__ //usb传输数据的宏定义描述 #include "sys.h" typedef __packed unio ...
 - iOS 代码实现获得应用的版本号(Version/Build)
			
http://www.open-open.com/lib/view/open1411817778203.html
 - mysql查看sql语句执行时间
			
原文地址: http://www.cnblogs.com/happySmily/p/5943311.html
 - 批量检查APK是否具有指定的权限。
			
为测试组的妹子提供的. 效果如下: 目录结构如下: 源代码思路: 1.将apk文件变为zip文件.这里是修改后缀 2.解压文件到指定目录.可以只解压其中mainfest.xml文件 3.移动xml文件 ...