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 ...
随机推荐
- svn版本库通过svn://127.0.0.1/不能导出的问题解决了!!
svn:本地file:///E:/SvnServerHome没问题,但是换为svn://192.168.1.100/SvnServerHome 报异常. 配置http协议访问svn 原文:http:/ ...
- Transport layer and Network layer
http://stackoverflow.com/questions/13333794/networking-difference-between-transport-layer-and-networ ...
- source command not found in sh shell解决办法
在Ubuntu系统中执行脚本的时候突然出现错误"source command not found in sh shell" 这个其实在Ubuntu 当中 执行脚本默认的使用的是da ...
- 函数之局部变量和使用global语句
局部变量当你在函数定义内声明变量的时候,它们与函数外具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是 局部 的.这称为变量的 作用域 .所有变量的作用域是它们被定义的块,从它们的名称被定义 ...
- AOP:代理实现方式①通过继承②通过接口
文件结构: 添加日志: package com.wangcf.manager; public class LogManager { public void add(){ System.out.prin ...
- 对position的理解
作者:zccst 先看看手册 值 描述 absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "t ...
- JQuery EasyUI DataGrid 获取属性值
在Jquery EasyUI中返回操作的时候,根据当前页返回到数据选取页: var grid = $('#datagrid'); var options = grid.datagrid('getPag ...
- leetcode--002 rpn
package leetcode; import java.util.Stack; public class RPN { public static int evalRPN(String[] toke ...
- 谈谈MySQL数据表的类型(转)
谈谈MySQL数据表的类型 通常意义上,数据库也就是数据的集合,具体到计算机上数据库可以是存储器上一些文件的集合或者一些内存数据的集合. 我们通常说的MySql数据库,sql server数据库等等其 ...
- 升级R版本后,更新Package
升级R版本后,若重新安装所有的package将非常麻烦,可以尝试运行一下程序: 1)在旧版本中的R中运行 #--run in the old version of R setwd("C:/T ...